一会喝、DOM
DOM(Document Object Model) 文檔對(duì)象模型
操作文檔
元素=標(biāo)簽=節(jié)點(diǎn)
DOM樹(節(jié)點(diǎn)關(guān)系)
oEle.tagName 獲取元素的名字
節(jié)點(diǎn):
標(biāo)簽節(jié)點(diǎn)
文本節(jié)點(diǎn)
判斷節(jié)點(diǎn)類型
oEle.nodeType
1 標(biāo)簽節(jié)點(diǎn)
3 文本節(jié)點(diǎn)
9 document
二列荔、獲取元素
通過id獲取一個(gè)元素
document.getElementById()
通過標(biāo)簽名獲取一個(gè)元素
oEle.getElementsByTagName()
通過class獲取一個(gè)元素
oEle.getElementsByClassName()
html
document.documentElement
body
document.body
通過節(jié)點(diǎn)關(guān)系獲取
獲取子節(jié)點(diǎn)
獲取第一層子節(jié)點(diǎn)
oParent.children
獲取標(biāo)簽和文本節(jié)點(diǎn)
oParent.childNodes(不推薦)
獲取父節(jié)點(diǎn)
獲取結(jié)構(gòu)上父級(jí)
obj.parentNode
最大是document,再往上是null
獲取定位上父級(jí)
obj.offsetParent
最大是body肾请,再往上是null
獲取兄弟節(jié)點(diǎn)
上一個(gè)兄弟節(jié)點(diǎn)
兼容高級(jí)瀏覽器
oEle.previousElementSibling
兼容IE678
oEle.previousSibling
兼容寫法
oEle.previousElementSibling||oEle.previousSibling
下一個(gè)兄弟節(jié)點(diǎn)
兼容高級(jí)瀏覽器
oEle.nextElementSibling
兼容IE678
oEle.nextSibling
兼容寫法
oEle.nextElementSibling||oEle.nextSibling
獲取首尾子節(jié)點(diǎn)
獲取首子節(jié)點(diǎn)
兼容高級(jí)瀏覽器
oParent.firstElementChild
兼容IE678
oParent.firstChild
兼容寫法
var oFirst = oParent.firstElementChild||oParent.firstChild;
oParent.children[0];
獲取尾子節(jié)點(diǎn)
兼容高級(jí)瀏覽器
oParent.lastElementChild
兼容IE678
oParent.lastChild
兼容寫法
var oFirst = oParent.lastElementChild||oParent.lastChild;
oParent.children[oParent.children.length-1]
三留搔、創(chuàng)建、添加铛铁、刪除隔显、克隆
元素
創(chuàng)建
document.createElement('標(biāo)簽名');
添加
添加到父級(jí)的最后
父級(jí).appendChild(子級(jí));
在某個(gè)元素之前插入
父級(jí).insertBefore(要插入的元素,誰之前);
刪除
父級(jí)中刪除子級(jí)
父級(jí).removeChild(子級(jí));
克隆
oEle.cloneNode(); 克隆元素
只有標(biāo)簽却妨,沒有內(nèi)容。
oEle.cloneNode(true); 深度克隆
所有東西全都克隆