DOM
Document Object Model 文档对象模型
获取元素,元素也叫标签
<div class='acitve' id='app'></div>
window.app
app
document.getElementById('app')
document.getElementsByTagName('div')[0]
document.getElementsByClassName('acitve')[0]
document.querySelector('#app')
document.querySelectorAll('.active')[0]
获取特定元素
document.documentElement
document.head
document.body
window
document.all
!!document.all
获取到的元素都是对象
console.dir(document.querySelectorAll('div')[0])可查看原型
- 自身属性,className,id,style等等
- 第一层原型HTMLDivElement.prototype,所有div共有的属性
- 第二层原型HTMLElement.prototype,所有HTML标签共有的属性
- 第三层原型Element.prototype,所有XML、HTML标签的共有属性
- 第四层原型Node.prototype,所有节点共有的属性,节点包含XML标签文本注释、HTML标签文本注释等
- 第五层原型EventTarget.prototype,最重要的函数属性为addEventLisener
- 最后一层为Object.prototype
节点和元素的区别,节点Node包括了以下几种
- x.nodeType得到一个数字
- 1表示元素Element,也叫标签Tag
- 3表示文本Text
- 8表示注释Comment
- 9表示文档Document
节点的增删改查
增
let div1 = document.createElement('div')
document.createElement('script')
document.createElement('style')
let text = document.createTextNode('jack')
div1.appendChild(text)
div1.innerHTML = 'hi'
div1.textContent = 'hi'
var insertedNode = parentNode.insertBefore(newNode, referenceNode);
<div id='divFater'>
<div id='big'></div>
</div>
let o = document.createElement('div')
divFater.insertBefore(o,big)
apendChild
<div id="test1"></div>
<div id="test2"></div>
let div1 = document.createElement('div')
div1.textContent = 'hahahaha'
test1.appendChild(div1)
test2.appendChild(div2)
div1.cloneNoed(true)
删
div1.parentNode.removeChild(div1)
div1 = null
test2.appendChild(div1)
div.remove()
改
写标准属性
div1.id = 'test'
div1.className = 'red blue'
div1.classList.add('blue')
div1.style = 'width:200px;color:red;'
div1.style.color = 'yellow'
div1.style.background-color
div1.style.backgroundColor = 'green'
div1.setAttribute('data-x','test')
div1.dataset.x = '222'
读标准属性
div.id
div.style
div.className
div.href
div.getAttribute('href')
改事件处理函数
- div.onclick === null //ture,默认为null
- 默认点击div不会有任何事情发生
- 如果把onclick赋值为一个函数
- 点击div时浏览器就会调用这个函数
- 并且是这样调用的fn.call(div,event)
- div会被当做this
- event则包含了点击事件的所有信息,如坐标
- div.addEventListener //
改内容
- div.innerText = 'xx'
- div.textContent = 'xx'
- 两者几乎没差别
div.innerHTML="<strong>i</strong> //改HTML内容
- 改标签
div.innnerHTML = ''
div.appendChild(newChild)
- 改爸爸
newParent.appendChild('div')因为元素只能存在一个地方,原来的位置会消失
查
div.parentNode
div.parentElement
div.parentNode.parentNode
div.parentElement.parentElement
node.childNodes
node.children
let allSiblings = div1.parentNode.children
let notMeList = []
for(let i=0;i<allSiblings.length;i++){
if(allSiblings[i]!==div1){
notMeList.push(allSiblings[i])
}
}
div1.parentElement.children[0]
div1.firstChild
div1.parentElement.children[div1.parentElement.children.length-1]
div1.lastChild
div1.previousSibling
div1.previousElementSibling
div1.nextSibling
div1.nextElementSibling
DOM操作是跨线程的
跨线程操作
- 各线程各司其职
- JS引擎不能操作页面,只能操作JS
- 渲染引擎不能操作JS,只能操作页面
跨线程通信
- document.body.appendChild(div1)
- 当浏览器发现JS在body里面加了div1对象
- 浏览器会通知渲染引擎在页面里新增一个div元素
- 新增的div元素所有属性都照抄div1
渲染合并
<div id='app'></div>
.star{
border:1px red solid;
width:100px;
height:100px;
transition:width 1s;
}
.end{
width:200px;
}
app.classList.add('star')
app.classList.add('end')
app.classList.add('star')
app.clientWidth
app.classList.add('end')
属性同步
- 标准属性 id,className,title等会自动同步
- data-*属性同上
- 非标准属性,如自己设置的jkj='111',只会停留在js线程内,不会同步到页面中
<div id='a' data-j='b' jk='c'></div>
let div = document.querySelector('#a')
div.id = 'qwq'
div.dataset.j = 'asdsa'
div.jk = 'rtrt'
Property vs Attribute
- js线程中div的所有属性叫做div的property
- 渲染引擎中div对应标签的属性,叫做attribute
区别
- 大部分时候,同名的propery和attribute的值相等
- 如果不是标准属性只在一开始相等,后续更改无效
- attribute只支持字符串