這是一份邊學邊記的 JavaScript 速查筆記,把散落的語法要點整理成可以隨時翻閱的清單。內容偏基礎,適合當作初學階段的對照表,或複習時的快速掃描。
document.write("") 直接輸出內容,不換行;document.writeln() 則會換行。//,多行註解為 /* ... */。<!-- //--> 包住整段程式。<script type="text/javascript" src="aaa.js"></script>;不支援腳本的瀏覽器則用 <noscript></noscript> 顯示替代內容。document.close()。document -> html -> (head, body)。window -> (navigator, screen, history, location, document)。document.getElementById("元素ID").name(或 .value)。document.forms[0]、document.forms[1] 依序存取,表單總數為 document.forms.length。window.open() 開新窗、window.close() 關閉、self 代表目前視窗、window.focus() 置於最前、blur() 失去焦點。parent 是父視窗,top 是最上層視窗,opener 是開啟目前視窗的那個視窗(可多層串接 opener.opener)。frames[n].物件、frames["frameName"].物件、frameName.物件。window.alert("字串") 提示訊息。window.confirm() 確認框,回傳布林值:if (confirm("你確定嗎?")) { alert("ok"); } else { alert("Not Ok"); }window.prompt("訊息", "預設值") 輸入框。window.status = "內容";預設文字則是 window.defaultStatus。window.print()。window.onbeforeunload = verifyClose;,在函式中設定 event.returnValue 即可顯示提醒。var;定義常數用 const。parseInt()、parseFloat();數字轉字串:"" + 變數。isNaN(變數) 用來測試是否為非數值。== 等於、!= 不等於、>、>=、<、<=。+,追加指定uttl用 +=。if (condition) {} else {}。for ([初始式]; [條件]; [更新式]) { ... },中斷用 break。function 函式名([參數], ...) { 敘述 }。javascript: 開頭再接函式名。var myString = new String("This is lightsword");,長度用 length。toUpperCase() / toLowerCase()。indexOf("子字串") 回傳第一次出現的位置(從 0 起算),lastIndexOf() 回傳最後一次;找不到時回傳 -1。charAt(9);取得 ASCII 碼:charCodeAt(n)。substring(2, 6) 取起點到終點;substr(start[, length]) 取指定長度。concat(string2) 或 +=。match(正則)、replace(正則, 取代字串)、split(分隔符)(回傳陣列)。
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 定位。document.getElementById("元素").tabIndex = 1。select() 使元素文字處於選取狀態;onfocus="this.blur()" 可阻止使用者在文字框輸入。innerHTML 取得或設定元素內的 HTML,例如 <p id="para">how are <em>you</em></p> 的 innerHTML 就是 how are <em>you</em>。innerText 行為類似,但不會把 <em> 這類標記顯示出來。contentEditable 設定元素是否可編輯,isContentEditable 回傳目前狀態。isDisabled 判斷是否為停用狀態,disabled 則用來設定。document.createElement()、document.createTextNode()。document.all.tags("div") 可取該標籤的集合,.length 即為數量;document.all 代表其下全部元素。addBehavior() 用來掛載副檔名為 .htc 的外部行為檔。Math.PI、Math.SQRT2、Math.max(v1, v2)、Math.pow(v, 10)、Math.round(v) 四捨五入、Math.floor(Math.random() * (n + 1)) 取隨機整數。var today = new Date();getTime()、getYear()、getFullYear()(四位年)、getMonth()、getDate()、getDay()(星期幾)、getHours()、getMinutes()、getSeconds()。setTime()、setYear()、setMonth()、setDate()、setDay()、setHours()、setMinutes()、setSeconds()。window.onerror = doNothing;;自訂處理:window.onerror = handleError;\n。this.moveTo(0,0); this.outerWidth = screen.availWidth; this.outerHeight = screen.availHeight;window.location.href = "URL" 指定目前顯示的位置;window.navigate("URL") 亦可導向。location 的組成屬性:protocol(http:)、hostname(www.example.com)、port(80)、host(含端口)、pathname(/a/a.html)、hash(#錨點)、href(完整資訊)。window.location.reload()。window.history.back()、forward()、go(頁數或URL)。<a href="a.html" onclick="location.href='b.html'; return false"> 會停在 b.html。external.AddFavorite("網址", "名稱")。setInterval("expr", msecDelay) 或 setTimeout。window.showModalDialog()、window.showModeless()(僅 IE 支援)。window.scroll(x, y)、scrollBy。window.resizeTo(寬, 高)。onload() 首次載入、onunload() 關閉時。this 代表目前所屬的位置。這份筆記的價值不在背誦,而在建立「遇到什麼問題該查哪個屬性」的索引感。真正寫作的時候,還是得搭配 API 文件一起看。