以下整理 JavaScript 常用物件、屬性與事件,方便開發時快速查閱。
window 是瀏覽器中的頂層物件,包含 frames、self、parent、top、status、defaultStatus、name 等屬性,以及 alert、confirm、prompt、open、close 等方法。
location 用來取得與操作目前網址,常見屬性有 href、protocol、host、hostname、port、pathname、hash、search。
document 代表目前文件,常見屬性有 title、location、lastModified、referrer、bgColor、fgColor、forms、links、anchors。常用方法包括 write、writeln、clear、close。
表單物件包含 name、method、action、target、elements、length 等屬性,並可透過 submit 提交表單,或透過 onSubmit 攔截提交行為。
text、textarea、checkbox、radio、select、button、password 等元素各有 value、name、checked、selectedIndex 等屬性,也可使用 focus、blur、select、click 等方法。
navigator 提供 appName、appVersion、userAgent 等資訊,history 則可用來控制瀏覽歷史,例如 history.go(-1)。
String 提供 charAt、indexOf、substring、toLowerCase、toUpperCase 等方法。Date 提供 getFullYear、getMonth、getDate、getHours、getTime 等方法。Math 提供 abs、ceil、floor、round、max、min、random、sqrt 等方法。
滑鼠事件包括 click、dblclick、mousedown、mouseup、mouseover、mouseout。鍵盤事件包括 keydown、keypress、keyup。表單事件包括 change、submit、reset、select、focus、blur。
event.clientX 與 event.clientY 可取得滑鼠點擊座標,event.offsetX 與 event.offsetY 可取得目前懸停座標。window 還提供 scroll、resizeTo、moveTo、focus、open 等方法。
舊版 IE 的 filter 與 cursor 樣式也常被收錄在速查表中,但現代開發應優先使用標準 CSS 與 DOM API。