dom操作js

來源:互聯網
上載者:User

標籤:des   io   ar   for   div   sp   on   c   ef   

1、getElementById()//ie8下不區分大小寫

    getElementsByName()//name屬性相比於Id可以不唯一,且其只在表單。表單元素。img 。iframe中有用,返回一個nodelist數組、

  getElementsByTagName()//返回數組

getElementsByClassName()//根據css屬性選取元素。除了IE8以下都實現了

2、通過css選取器來選擇文檔要素

與上面的選擇方法相比,下面的方法是靜態,不會隨著文檔的變化而改變

querySelectorAll(“div.middle”)//返回一個NodeList

querySelector()//返回第一個匹配的元素

3、文檔節點樹:

parentNode

childNodes firstChild  lastChild

nextSibling  previoursSibling  previousSibling

nodeType//節點類型為整數

nodeValue//text或comment的常值內容

nodeName//標籤名,為大寫

4、文件項目樹

只考慮元素對象,而不考慮文本和注釋等,其在實際操作時,往往更有用.除了ie4都實現了

children//NOdeList

firstElementChild    lastElementChild

nextElementSibling previousElementSibling

childElementCount

5、設定元素屬性

對於標準屬性如:id、事件、src等。直接用屬性賦值即可

對於非標準屬性:class等。用下面的方法:、

getAttribute()   setAttribute()   hasAttribute()  removeAttribute()

6element.textContent//可以用來設定純文字

7、修改文檔樹

建立節點:document.createElement()createTextNode()

插入節點    parent.appendChild(chid)    parent.insertBefore(child,parent.childNodes[n])//如果child是文檔中存在的接待你,則會將其破移動。刪除其原在位置

8、刪除、複製、替代節點

removeChild   cloneNode  replaceChild

 

dom操作js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.