DIV CSS 佈局教程網

JavaScript 入門(十):連結、表單與表單元素物件
編輯:關於JavaScript     

anchors[] 與 links[]

用法:document.anchors[x]、document.links[x]

document.anchors 是一個陣列,依文件中的出現順序(從 0 起算)收錄所有帶 name 屬性的 <a> 標籤。document.links 則收錄所有帶 href 屬性的 <a> 標籤,以及 <map> 區段中的 <area> 標籤。

若某個 <a> 同時具備 name 與 href,它會同時被算進這兩個陣列——既是 Anchor 物件,也是 Link 物件。

在 IE 中,為 <a> 加上 id="..." 屬性後,可直接用該 id 名稱來引用物件;許多文件元件都支援這招,但同一頁中 id 不可重複。

Link 物件的屬性

protocol、hostname、port、host、pathname、hash、search、href 與 location 物件相同;target 對應連結的目標視窗。單一 Anchor 物件則沒有屬性。

事件:onclick、onmouseover、onmouseout、onmousedown、onmouseup

applets[] 與 embeds[]

document.applets[] 收錄文件中所有的 Java Applet;document.embeds[] 收錄所有 <embed> 外掛。兩者都是陣列,而各個 Embed 物件因為外掛種類不同,擁有的屬性與方法也不一樣。

forms[]:表單物件

document.forms[] 收錄文件中所有 <form>。用 document.forms[x] 可以取出單一表單,但更常見的做法是在 <form> 上加上 name 屬性,之後直接用 document.<表單名> 引用。

屬性

name 表單名稱;action 提交位址;method 提交方法;target 提交後回應的目標視窗;encoding 提交內容的編碼方式(對應 enctype);length 表單內元素數量。

方法

reset() 重置表單,等同按下「重設」;submit() 送出表單,等同按下「送出」。

事件:onreset、onsubmit

表單元素物件

從 Button 到 Textarea 都是表單的元素物件。引用方式統一為:

<文件物件>.<表單物件>.<元素名稱>

其中「元素名稱」就是該 <input> 標籤中 name="..." 的值。

Button 按鈕物件由 <input type="button"> 建立,主要屬性為 name 與 value(按鈕上顯示的文字)。

實務提醒:以 document.forms[0] 這種硬編碼索引的方式取表單,在頁面上增減表單後很容易出錯。建議一律加上 name 或使用 id 定位,程式碼意圖也更清楚。

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