這份速查表把日常編程最常用到的語法與方法整理成條列,方便隨時翻閱對照。
document.write(""):輸出語句。//。document.getElementById("元素ID").name(或 .value)。document.getElementById("output").value = document.getElementById("input").value.toUpperCase();parseInt()、parseFloat()。"" + 變數。length。+ 號。== 等於、!= 不等於、>、>=、<、<=。var 宣告變數。if (condition) {} else {}。for ([初始式]; [條件]; [更新式]) { 迴圈內容 }。break。function functionName([parameter], ...) { statement[s] }。document.forms[0]、document.forms[1] 取代。window.open()、關閉視窗 window.close()、視窗本身 self。window.status = "字元"。window.alert("字元")。window.confirm()。window.prompt()。window.location.href = "/blog/URL"。document.forms.length。document.close()。+=。document.createElement()、document.createTextNode()。document.getElementById()。把表單中所有文字欄位清空,可用迴圈搭配 type == "text" 判斷:
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 = "";
}
}
document.forms[0].checkThis.checked(回傳 true 或 false)。document.forms[0].groupName.length。checked。document.forms[0].selectName.options[n].value,也可用 selectedIndex 判定被選中的項。var myString = new String("This is lightsword");。string.toUpperCase()/string.toLowerCase()。String1.indexOf("String2"),找不到回傳 -1。StringA.charAt(9)。stringA.substring(2, 6)。Math.PI(圓周率)、Math.SQRT2、Math.max()、Math.pow()、Math.round()(四捨五入)、Math.floor(Math.random() * (n + 1))(隨機數)。以上是速查表的上半部,下半部將繼續補充更多常用語法。