DIV CSS 佈局教程網

JavaScript 語法與常用 API 速查整理(100+ 條)
編輯:關於JavaScript     

這份整理收錄了 JavaScript 最基礎的語法與常用 API,適合當作日常開發的速查表。內容偏向早期瀏覽器環境,部分項目在現代開發中已不建議使用,但作為理解語言基礎仍有參考價值。

基本語法與型別

1. document.write("") 為輸出語句
2. JS 中的註解為 //
3. 傳統的 HTML 文件順序是:document → html →(head, body)
4. 一個瀏覽器視窗中的 DOM 順序是:window →(navigator, screen, history, location, document)
5. 取得表單中元素的名稱和值:document.getElementById("元素ID").name(或 value)
6. 小寫轉大寫:document.getElementById("output").value = document.getElementById("input").value.toUpperCase();
7. JS 中的值型別:String, Number, Boolean, Null, Object, Function
8. 字串轉數值:parseInt()、parseFloat()
9. 數值轉字串:""+變數
10. 取字串長度:length
11. 字串串接使用 + 號
12. 比較運算子:== 等於、!= 不等於、>、>=、<、<=
13. 宣告變數使用 var
14. 判斷敘述:if(condition){}else{}
15. 迴圈結構:for([initial expression];[condition];[update expression]) {inside loop}
16. 迴圈中止:break
17. 函式定義:function functionName([parameter],...){statement[s]}
18. 多個 form 時可用 document.forms[0]、document.forms[1] 取得
19. 視窗:開啟 window.open()、關閉 window.close()、視窗本身 self
20. 狀態列:window.status="字串"
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()

DOM 與元素操作

49. 引用外部 JS:<script type="text/javascript" src="aaa.js"></script>
50. 在不支援腳本的瀏覽器中顯示的 HTML:<noscript></noscript>
51. 超連結與 onclick 並存時,舊版瀏覽器前往 a.html,否則前往 b.html:<a href="a.html" onclick="location.href='b.html';return false">dfsadf</a>
52. 內建物件:Array, Boolean, Date, Error, EvalError, Function, Math, Number, Object, RangeError, ReferenceError, RegExp, String, SyntaxError, TypeError, URIError
53. 換行:\n
55. all 代表其下層的全部元素
56. 焦點順序:document.getElementById("表單元素").tabIndex = 1
57. innerHTML 取得的是含標籤的內容;例如 <p id="para">"how are <em>you</em>"</p> 的 innerHTML 就是 how are <em>you</em>
58. innerText 同上,但不會把 <em> 這類標記顯示出來
59. contentEditable 可設定元素是否可編輯,isContentEditable 回傳是否可編輯的狀態
60. isDisabled 判斷是否為停用狀態,disabled 設定停用狀態
61. length 取得長度,回傳整數
62. addBehavior() 是一種 JS 呼叫的外部函式檔,副檔名為 .htc
63. window.focus() 使目前視窗移到最前方
64. blur() 指失去焦點,與 focus() 相反
65. select() 指元素為選取狀態
66. 防止使用者對文字框輸入文字:onfocus="this.blur()"
67. 取得該元素在頁面中出現的數量:document.all.tags("div").length
68. 兩種視窗輸出:模態與非模態,window.showModalDialog()、window.showModeless()
69. 狀態列文字:window.status='文字';預設狀態列文字:window.defaultStatus = '文字.'
70. 加入我的最愛:external.AddFavorite("https://www.google.com","Google")
71. 遇到腳本錯誤時不做任何處理:window.onerror = doNothing;;指定錯誤處理常式:window.onerror = handleError;
72. 指定目前視窗的父視窗:window.opener,支援 opener.opener... 的多重續接
73. self 指的是目前的視窗
74. 狀態列顯示內容:window.status="內容"
75. top 指的是框架集中最頂層的框架
76. 關閉目前視窗:window.close()
77. 確認框:if(confirm("Are you sure?")){alert("ok");}else{alert("Not Ok");}
78. 視窗重新導向:window.navigate("https://www.google.com")
79. 列印:window.print()
80. 提示輸入框:window.prompt("message","defaultReply")
81. 視窗捲動條:window.scroll(x,y)
82. 視窗捲動到位置:window.scrollby
83. 設定時間間隔:setInterval("expr",msecDelay) 或 setInterval(funcRef,msecDelay)、setTimeout
84. 模態顯示在 IE4+ 可用,在 NN 中不行:showModalDialog("URL"[,arguments][,features])
85. 離開之前使用的處理常式:function verifyClose(){event.returnValue="we really like you and hope you will stay longer.";}} window.onbeforeunload=verifyClose;
86. 視窗第一次呼叫時的處理常式:onload()
87. 視窗關閉時呼叫的處理常式:onunload()
88. window.location 的屬性:protocol(http:)、hostname(www.example.com)、port(80)、host(www.example.com:80)、pathname("/a/a.html")、hash("#giantGizmo",指跳到相應的錨記)、href(全部資訊)
89. window.location.reload() 重新整理目前頁面
89-1. parent.location.reload() 重新整理父視窗物件(用於框架)
89-2. opener.location.reload() 重新整理開啟視窗的父視窗(用於單開視窗)
89-3. top.location.reload() 重新整理最頂端物件(用於多開視窗)
90. window.history.back() 返回上一頁、window.history.forward() 前往下一頁、window.history.go() 返回第幾頁(也可使用走訪過的 URL)
91. document.write() 不換行輸出、document.writeln() 換行輸出
92. document.body.noWrap=true; 防止連結文字換行
93. 變數.charAt(第幾位) 取該變數指定位置的字元
94. "abc".charCodeAt(第幾個) 回傳該字元的 ASCII 碼
95. 字串串接:string.concat(string2),或用 +=
96. 變數.indexOf("字元",起始位置) 回傳第一次出現的位置(從 0 開始)
97. string.lastIndexOf(searchString[,startIndex]) 最後一次出現的位置
98. string.match(regExpression) 判斷字串是否匹配
99. string.replace(regExpression,replaceString) 替換現有字串
100. string.split(分隔符) 回傳一個陣列
101. string.substr(start[,length]) 取從指定位置開始、指定長度的字串
102. string.toLowerCase() 全部轉小寫
103. string.toUpperCase() 全部轉大寫
104. parseInt(string[,radix]) 強制轉為整數
105. parseFloat(string[,radix]) 強制轉為浮點數
106. isNaN(變數) 測試是否為數值型
107. 定義常數的關鍵字:const;定義變數的關鍵字:var

使用這份速查表時的提醒

上表中有幾項在現代專案中已不建議使用,值得特別標記出來:

  • document.write():會清空整個文件,且在非同步情境下行為難以預期,請改用 DOM 操作。
  • showModalDialog():已被淘汰,請改用 <dialog> 元素或自製 Modal 元件。
  • window.navigate():僅 IE 支援,請改用 location.href。
  • document.all、addBehavior():皆為 IE 專屬,分別應以 querySelectorAll 與標準事件模型取代。
  • innerText:非標準屬性,且會觸發 reflow,請優先使用 textContent。
  • == 與 !=:會進行型別轉換,實務上請一律使用 === 與 !==。
  • substr():已標記為 deprecated,請改用 substring() 或 slice()。

這份清單的價值在於快速回顧語法,但實際寫作時請以目前的標準文件為準。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved