DOM(Document Object Model)
Node 分為 Document(html)堪夭、Element(元素)和 Text(文本)咏删,以及其他不重要的惹想。
Node相當(dāng)于是Document、Element督函、Text的根元素嘀粱,Node具有的屬性,Document辰狡、Element锋叨、Text都具有,但Document宛篇、Element娃磺、Text又各自具有自己的屬性(這跟Array、Function與Object的關(guān)系是一樣的些己,Array豌鸡、Function都具有Object的公用屬性,但它們又有各自的特性)
Node 的接口
Node具有的接口段标,Document涯冠、Element、Text也都具有逼庞。
-
屬性
childNodes, firstChild, innerText, lastChild, nextSibling, nodeName, nodeType, nodeValue, outerText, ownerDocument, parentElement, parentNode, previousSibling, textContent
媽的記不住蛇更。
如果記不住就背下以下單詞:- child / children / parent
- node
- first / last
- next / previous
- sibling / siblings
- type
- value / text / content
- inner / outer
- element
然后互相組合
document.body.childNodes會(huì)把回車(chē)也當(dāng)作子節(jié)點(diǎn)
document.body.firstChild也會(huì)把回車(chē)當(dāng)子節(jié)點(diǎn)
document.body.lastChild也會(huì)把回車(chē)當(dāng)子節(jié)點(diǎn)
document.body.previousSibling也會(huì)把回車(chē)當(dāng)子節(jié)點(diǎn)
document.body.nextSibling也會(huì)把回車(chē)當(dāng)子節(jié)點(diǎn)
document.body.children不會(huì)把回車(chē)當(dāng)子節(jié)點(diǎn)
document.body.firstElementChild不會(huì)把回車(chē)(文本)當(dāng)子節(jié)點(diǎn)
document.body.lastElementChild不會(huì)把回車(chē)(文本)當(dāng)子節(jié)點(diǎn)
document.body.previousElementSibling不會(huì)把回車(chē)(文本)當(dāng)子節(jié)點(diǎn)
document.body.nextElementSibling不會(huì)把回車(chē)(文本)當(dāng)子節(jié)點(diǎn)
firstElementChild、lastElementChild赛糟、previousElementSibling不是Node的屬性派任,是Element的屬性。
nodeName都是大寫(xiě)璧南,除了svg掌逛,HTML要寫(xiě)成document.documentElement.nodeName(死記硬背)
nodeType取值:
nodeType = 1的Node就是Element;nodeType = 3的Node就是Text司倚;nodeType = 8的Node就是Comment豆混;nodeType = 9的Node就是Document
考點(diǎn):DocumentFragment性能優(yōu)化
innerText和textContent區(qū)別:
- 方法(如果一個(gè)屬性是函數(shù),那么這個(gè)屬性就也叫做方法动知;換言之皿伺,方法是函數(shù)屬性)
- appendChild()
- cloneNode()
- contains()
- hasChildNodes()
- insertBefore()
- isEqualNode()
- isSameNode()
- removeChild()
- replaceChild()
- normalize() // 常規(guī)化
cloneNode():
存在深拷貝和淺拷貝
Document 接口
- 屬性
body
characterSet
childElementCount
children
doctype
documentElement
domain
fullscreen
head
hidden
images
links
location
onxxxxxxxxx
origin
plugins
readyState
referrer
scripts
scrollingElement
styleSheets
title
visibilityState
2.方法:
close()
createDocumentFragment()
createElement()
createTextNode()
execCommand()
exitFullscreen()
getElementById()
getElementsByClassName()
getElementsByName()
getElementsByTagName()
getSelection()
hasFocus()
open()
querySelector()
querySelectorAll()
registerElement()
write()
writeln()
Element 接口
自己看mdn