document常用屬性及屬性集合,document集合

來源:互聯網
上載者:User

document常用屬性及屬性集合,document集合


    上一篇博文說刪除結點資訊時,有一行代碼document.body.removeChild(deleteNode),那document.body是什麼呢?body跟document什麼關係呢?今天說說常用的document屬性和集合屬性。所謂document屬性是指那些單的屬性資訊,它指代的是單個的對象;集合屬性指的是document對象裡面的那些可以歸為集合的子物件。


document.title


<html><head><title>Attribute</title></head><body><script>alert(document.title);</script></body></html>

    上面的代碼,會在頁面載入完成後,彈一個alert框出來,那裡面內容是什麼呢?

    執行以下,看看結果

   

    彈出了title的內容,那說明document.title 指向了html頁面中title標籤的內容。

    打個斷點看看,document還有哪些屬性資訊?

    斷點打到alert(document.title);行,並對document進行監視

         


    展開document對象,可以看到

         


    document的屬性和集合屬性太多,暫時先列這幾個看看。


document.bgColor


    像document.bgColor 表示背景顏色,當前值為十六進位表示的#ffffff,我們給頁面添加個背景色,然後再將背景色列印出來看看,將代碼修改為

<html><head><title>Attribute</title></head><body bgColor="#CCDDAA"><script>alert(document.bgColor);</script></body></html>

    彈出框效果為

         


document.anchors


    一個集合類的屬性document.anchors 擷取所有的超連結的集合

<html><head><title>Attribute</title></head><body bgColor="#CCDDAA"><a href="http://www.baidu.com" name="clj">百度</a></br><a href="http://www.google.com" name="clj">Google</a></br><a href="http://www.gougou.com" name="clj">狗狗</a></br><script>alert(document.anchors.length);</script></body></html>

    彈出框值為3

         


    這個樣本有個奇怪的地方,就是超連結裡面必須添加name屬性,document.anchors才能擷取到超連結,否則document.anchors 拿到的集合為空白集合。如果有知道的朋友,可以說一下原因。

document.forms

 

   再看一個集合屬性document.forms 擷取所有的form表單的集合

<html><head><title>Attribute</title></head><body bgColor="#CCDDAA"><form id="form1"></form><form id="form2"></form><script>alert(document.forms.length);</script></body></html>

    彈出框值為2,樣本中有兩個form表單。

 

    擷取集合類屬性中的單個對象方法

    接著上面的代碼,假如我們想要擷取到第一個form表單的id屬性值,該如何擷取呢?

    集合類屬性的結果都是集合,都可以通過數組下標的方式來擷取,

即 

var forms = document.forms;for (i=0; i<forms.length; i++){alert(forms[i].id);}

    其次,我們斷點下集合屬性。

         


    可以看到其中的item() 方法,item方法傳遞的參數為對象在集合中的序號

    將alert(forms[i].id); 修改為 alert(forms.item(i).id); 同樣出效果。


    基本上,集合屬性通過這兩種方式訪問就可以了,如果開發中遇到更多的問題,想擷取更多的訪問方式,那請通過上面的斷點的方式,查看具體有哪些操作方法,然後再想辦法入手問題。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.