DOM選擇器,即查找標簽對象
阿新 • • 發佈:2018-12-19
集合 圖片 ext element sel spl play lap 獲取
一、查找元素
1、直接查找
document.getElementById(‘id1‘) //根據ID返回單個標簽 document.getElementsByName(‘n1‘) //根據name屬性獲取標簽集合 document.getElementsByClassName(‘c1‘) //根據class屬性獲取標簽集合 document.getElementsByTagName(‘div‘) //根據標簽名獲取標簽集合
2、間接查找
parentElement // 父節點標簽元素 children // 所有子標簽 firstElementChild //第一個子標簽元素 lastElementChild // 最後一個子標簽元素 nextElementtSibling // 下一個兄弟標簽元素 previousElementSibling // 上一個兄弟標簽元素
不常用:查找節點,節點包括:元素,文本,不包括屬性
parentNode // 父節點 childNodes // 所有子節點 firstChild // 第一個子節點 lastChild // 最後一個子節點 nextSibling // 下一個兄弟節點 previousSibling //查節點上一個兄弟節點
二、操作標簽
1、內容
innerText //文本 value //值
2、屬性
attributes // 獲取所有標簽屬性 setAttribute(key,value) // 設置標簽屬性 getAttribute(key) // 獲取指定標簽屬性
3、class
className // 獲取所有類名 classList.remove(cls) // 刪除指定類 classList.add(cls) // 添加類
DOM選擇器,即查找標簽對象