元素也叫标签
API
- window.idxxx 或者直接用idxxx
- document.getElementById('idxxx')
- document.getElementsByTagName('name')[0]
- document.getElementsByClassName('red')[0]
- document.querySelector('#idxxx')
- document.querySelectorAll('.red')[0]
注意点
- 工作中用5/6
- demo直接用1
- 兼容IE用2-4
获取特定元素
document.documentElement
document.head
document.body
window
document.all
节点的增删改查
增
let div1 = document.createElement('div');
document.createElement('style');
document.createElement('script');
document.createElement('li');
let text1 = document.createTextNode('你好');
div1.appendChild(text1)
div1.innerText='你好' 或者 div1.textContent='你好'
div1.appendChild('你好')
document.body.appendChild(div)
页面中已知元素.appendChild(div)
删
parentNode.childChild(childNode)
childNode.remove()
改
div.calssName = 'red blue'
div.classList.add('red')
div.style='width:100px; color:blue;'
div.style.width='200px'
div.style.backgroundColor='white'
div.dataset.x='frank'
读标准属性
div.calssList / a.href
div.getAttribute('class')/a.getAttribute('href')
改内容
div.innerText='xxx'
div.textContent='xxx'
div.innerHTML='<strong>重要内容</strong>'
div.innerHTML=''
div.appendChild(div2)
newParent.appendChild(div)
查
node.parentNode / node.parentElement
node.parentNode.parentNode
node.chiildNodes/node.children
node.parentNode.childNodes
node.parentNode.children
node.firstChild
bode.lastChild
node.previousSibling
node.nextSibling