1. 程式人生 > >JS常用屬性大全(二)

JS常用屬性大全(二)

21.彈出提示資訊:window.alert("字元");

22.彈出確認框:window.confirm();

23.彈出輸入提示框:window.prompt();

24.指定當前顯示連結的位置:window.location.href="URL"

25.取出窗體中的所有表單的數量:document.forms.length

26.關閉文件的輸出流:document.close();

27.字串追加連線符:+=

28.建立一個文件元素:document.createElement(),document.createTextNode()

29.得到元素的方法:document.getElementById()

30.設定表單中所有文字型的成員的值為空:

var form = window.document.forms[0]

for (var i = 0; i<form.elements.length;i++){

      if (form.elements[i].type == "text"){

          form.elements[i].value = "";

      }

}

31.複選按鈕在JS中判斷是否選中:document.forms[0].checkThis.checked (checked屬性代表為是否選中返回TRUEFALSE)

32.單選按鈕組(單選按鈕的名稱必須相同):取單選按鈕組的長度

document.forms[0].groupName.length

33.單選按鈕組判斷是否被選中也是用checked.

34.下拉列表框的值:document.forms[0].selectName.options[n].value (n有時用下拉列表框名稱加上.selectedIndex來確定被選中的值)

35.字串的定義:var myString = new String("This is lightsword");

36.字串轉成大寫:string.toUpperCase(); 字串轉成小寫:string.toLowerCase();

37.返回字串2在字串1中出現的位置:String1.indexOf("String2")!=-1

則說明沒找到.

38.取字串中指定位置的一個字元:StringA.charAt(9);

39.取出字串中指定起點和終點的子字串:stringA.substring(2,6);

40.數學函式:Math.PI(返回圓周率),Math.SQRT2(返回開方),Math.max(value1,value2)返回兩個數中的最在值,Math.pow(value1,10)返回value1的十次方,Math.round(value1)四捨五入函式,Math.floor(Math.random()*(n+1))返回隨機數