JS DOM 編程

什么是DOM

  • DOM(Document Object Model)是文檔對(duì)象模型。
  • JS 通過 Window.document 對(duì)象來操作網(wǎng)頁(yè)中的元素。

獲取元素

window.id 或者直接 id,這種方法比較少用。

// 以下幾種獲取元素的方法一般在 IE 瀏覽器中使用
document.getElementById('id') // 獲取 id 對(duì)應(yīng)的標(biāo)簽
document.getElementsByTagName('tagName')  // 獲取給定標(biāo)簽名稱的元素集合
document.getElementsByTagName('tagName')[index]  // 獲取給定標(biāo)簽名稱的元素集合中對(duì)應(yīng)索引的元素
document.getElementsByClassName('className')  // 獲取給定類名的元素集合
document.getElementsByClassName('className')[index]  // 獲取給定類名的元素集合中對(duì)應(yīng)索引的元素
// 以下幾種獲得元素的方法通過用在除了 IE 瀏覽器的其他瀏覽器

 // 獲取第一個(gè)對(duì)應(yīng)的 id ,類名,標(biāo)簽名對(duì)應(yīng)的元素
document.querySelector('#id' | '.className' | 'tagName') 

 // 獲取對(duì)應(yīng)的 id ,類名,標(biāo)簽名 對(duì)應(yīng)的元素集合
document.querySelectorAll('#id' | '.className' | 'tagName') 

 // 獲取對(duì)應(yīng)的 id ,類名,標(biāo)簽名對(duì)應(yīng)的元素集合中對(duì)應(yīng)索引的元素
document.querySelectorAll('#id' | '.className' | 'tagName') [index]

document.documentElement 獲取 html 元素
document.body 獲取 html 中的 body 節(jié)點(diǎn)
document.head 獲取 html 中的 head 節(jié)點(diǎn)
document.all 獲取頁(yè)面中的所有元素的集合,document.all 為 falsy 值,只有在 IE 瀏覽器中為 true,用來判斷瀏覽器是否是 IE 瀏覽器。

節(jié)點(diǎn)的增刪改查

節(jié)點(diǎn)包括元素和文本,元素就標(biāo)簽。

增加節(jié)點(diǎn)

// 創(chuàng)建一個(gè)標(biāo)簽節(jié)點(diǎn)
let div1 = document.createElement('div')

// 創(chuàng)建一個(gè)文本節(jié)點(diǎn)
let text1 = document.createTextNode('你好,JS')

// 向節(jié)點(diǎn)中插入文本
div1.appendChild(text1)

div1.innerText = '你好,JS'( IE )或者 div1.textContent = '你好'
// 但不能使用 div1.appendChild('你好,JS')

// 將節(jié)點(diǎn)插入頁(yè)面中,才能顯示,因?yàn)榇藭r(shí)的節(jié)點(diǎn)在 JS 線程中
document.body.appendChild(div1)

刪除節(jié)點(diǎn)

舊方法
parentNode.removeChild(childNode)
新方法
childNode.remove() 此時(shí)元素只是從頁(yè)面中刪除,節(jié)點(diǎn)仍在 JS 線程中,可以通過 parendNode.apendChild(childNode) 重新將節(jié)點(diǎn)添加到頁(yè)面中。
如果要徹底刪除 childNode 節(jié)點(diǎn),需要執(zhí)行 childNode = null。

改屬性

寫標(biāo)準(zhǔn)屬性

  • 改 class: div.className = 'red blue' 全覆蓋
  • 改 class: div.classList.add('red') 添加
  • 改 style: div.style = 'width: 100px; color : blue;'
  • 改 style 的一部分: div.style.width = 200px
  • 大小寫: div.style.backgroundColor = 'white'
  • 改 data-x 屬性: div.dataset.x = 'frank

讀標(biāo)準(zhǔn)屬性

  • div.classList | a.href
  • div.getAttribute('class') | a.getAttribute('href')
  • 兩種方法都可以,只是值可能稍微有些不同。

設(shè)置屬性
div.setAttribute(name, value)

改事件處理函數(shù)
div.onclick = function
div.onclick 默認(rèn)為 null,點(diǎn)擊后什么都不會(huì)發(fā)生。

改標(biāo)簽內(nèi)容
div.innerText = 'xxx'
div.textContent = 'xxx'

改 HTML 內(nèi)容

div.innerHTML = `<p> </p>` 

改標(biāo)簽

div.innerHTML = '' // 先清空
div.appendChild(div2) // 再添加

改父節(jié)點(diǎn)
newParent.appendChild(div)

查詢節(jié)點(diǎn)

div  // 查自己
div.parentElement || div.parentNode  // 查父代
div.parentNode.parentNode  // 查祖代
div.childNodes || div.children  // 查子代
div.childNodes[index].childNodes  // 查孫代

通過 div.children 查詢節(jié)點(diǎn),它的 length 會(huì)實(shí)時(shí)更新。
通過 document.querySelctorAll 查詢節(jié)點(diǎn),它的 length 不會(huì)實(shí)時(shí)更新。

div.parentNode.children 排除自己 查兄弟

let siblings = []
let c1 = div1.parentElement.children
for (let i = 0 ; i < c1.length ; i++) {
    if (c1[i] !== div1) {
      siblings.push(c1[i])
    }
}
div.firstChild  // 查第一個(gè)子節(jié)點(diǎn)
div.lastChild  // 查最后一個(gè)子節(jié)點(diǎn)
div.previousSibling // 查前一個(gè)節(jié)點(diǎn)
div.nextSibling // 查后一個(gè)節(jié)點(diǎn)

div.firstChild 包括 text 元素, div.firstElementChild 不包括 text 元素

遍歷節(jié)點(diǎn)的所有元素

let travel = (node, fn) => {
    fn (node)
    if (node.children) {
        for (let i = 0; i < node.children.length ; i++) {
            travel (node.children[i], fn)
        }
    }
}

travel(div, (node) => console.log(node))

資料來源:饑人谷

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • DOM基礎(chǔ)概念、操作 DOM是什么 DOM全稱為The Document Object Model,應(yīng)該理解為是一...
    蛋炒飯_By閱讀 226評(píng)論 0 0
  • DOM基礎(chǔ)概念、操作 DOM是什么 DOM全稱為The Document Object Model,應(yīng)該理解為是一...
    Arroganter閱讀 702評(píng)論 0 0
  • 文檔邏輯結(jié)構(gòu) DOM基礎(chǔ)概念、操作DOM是什么DOM全稱為The Document Object Model,應(yīng)該...
    數(shù)據(jù)萌新閱讀 370評(píng)論 0 0
  • 1. 相關(guān)概念 由 W3C 批準(zhǔn)并由所有于標(biāo)準(zhǔn)相兼容的 Web 瀏覽器支持的第三方技術(shù)成為 DOM (文檔對(duì)象模型...
    夢(mèng)回吹角連營(yíng)閱讀 455評(píng)論 0 3
  • DOM 編程 網(wǎng)頁(yè)其實(shí)是顆樹,JS如何操作這棵樹?JS操作不了。讓瀏覽器往window上加一個(gè)document。J...
    珍惜時(shí)間小李閱讀 621評(píng)論 0 0

友情鏈接更多精彩內(nèi)容