菜鸟javascript基础资料整理2

  1。

  //使用变量的属性

  <script type="text/javascript">

  var txt="Hello World!"

  document.write(txt.length)

  </script>

  2。

  //把字符串中的所有字母都被转化为大写字母。

  <script type="text/javascript">

  var str="Hello world!"

  document.write(str.toUpperCase())

  </script>

  3。

  //js中个变量添加超链接

  <script type="text/javascript">

  var txt="Hello World!"

  document.write("<p>超链接为: " + txt.link("http://www.w3school.com.cn") + "</p>")

  </script>

  4。

  //indexOf方法(定位字符串中某一个指定的字符首次出现的位置。如果没有查到返回-1,区分大小写)

  <script type="text/javascript">

  var str="Hello world!"

  document.write(str.indexOf("Hello") + "<br />") //1

  document.write(str.indexOf("World") + "<br />") //-1

  document.write(str.indexOf("world")) //6

  </script>

  5。

  //match() 方法

  //使用 match() 来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。

  <script type="text/javascript">

  var str="Hello world!"

  document.write(str.match("world") + "<br />") //world

  document.write(str.match("World") + "<br />") //null

  document.write(str.match("worlld") + "<br />") //null

  document.write(str.match("world!")) //world!

  </script>

  6。

  //replace() 方法在字符串中用某些字符替换另一些字符。

  var str="Visit Microsoft!"

  document.write(str.replace("Microsoft","W3School"))

  7.

  //合并两个数组

  <script type="text/javascript">

  var arr = new Array(3)

  arr[0] = "George"

  arr[1] = "John"

  arr[2] = "Thomas"

  var arr2 = new Array(3)

  arr2[0] = "James"

  arr2[1] = "Adrew"

  arr2[2] = "Martin"

  document.write(arr.concat(arr2))

  </script>