DOM基础知识点
DOM:document object model 文档对象模型
DOM获取元素的方法
1、document.getElementById('id')
2、document.getElementsByTagName('el')
3、document.getElementsByName('name')
4、document.getElementsByClassName('className')
获取多个的这几个方法,即使你获取的只有一个,它也是类数组,也是一个集合,如果想用其中的第一个,也要通过索引拿出来用 list[0]
5、 document.documentElement
document.body
6、在移动端我们获取元素常用的方法
document.querySelector() 获取一个
document.querySelectorAll() 获取多个
兼容所有浏览器的获取当前屏幕的宽度
let curWidth = document.documentElement.clientWidth || document.body.clientWidth;
let curHeight = document.documentElement.clientHeight || document.body.clientHeight;
DOM关系属性
● childNodes:获取所有的子节点
● children:获取所有的元素子节点
● parentNode:获取父亲节点
● previousSibling:获取上一个兄弟节点(哥哥)
● nextSibling:获取下一个兄弟节点(弟弟)
● firstChild:获取所有节点中的第一个
● lastChild:获取所有节点中的最后一个
let box = document.getElementById('box');
console.log(box.childNodes)
console.log(box.children)
console.log(box.parentNode)
console.log(box.previousSibling)
console.log(box.nextSibling)
console.log(box.firstChild)
console.log(box.lastChild)
function getMyChildren(ele,tagName){
let ary = [] , nodes = ele.childNodes
for(let i = 0; i < nodes.length; i++){
let cur = nodes[i]
if(cur.nodeType === 1){
if(tagName){
if(cur.nodeName.toLowerCase() === tagName.toLowerCase()){
ary.push(cur)
}
}else{
ary.push(cur)
}
}
}
return ary
}
let box = document.getElementById('box');
console.log(box)
let arr = getMyChildren(box,'ul')
console.log(arr)
封装节点操作方法
function prev(ele){
let pre = ele.previousSibling
while(pre && pre.nodeType !== 1){
pre = pre.previousSibling
}
return pre
}
function prevAll(ele){
let pre = ele.previousSibling
let newPre = []
while(pre){
if(pre.nodeType === 1){
newPre.unshift(pre)
}
pre = pre.previousSibling
}
return newPre
}
function next(ele){
let nex = ele.nextSibling
while(nex && nex.nodeType !== 1){
nex = nex.nextSibling
}
return nex
}
function nextAll(ele){
let nex = ele.nextSibling
let newNex = []
while(nex){
if(nex.nodeType === 1){
newNex.push(nex)
}
nex = nex.nextSibling
}
return newNex
}
function preAndNextAll(ele){
let pre = prevAll(ele)
let nex = nextAll(ele)
return pre.concat(ele,nex)
}
function preAndNex(ele){
let pre = prev(ele)
let nex = next(ele)
return [pre,ele,nex]
}
let li2 = document.getElementById('li3')
console.log(preAndNex(li2))
DOM元素的创建、插入、删除、克隆和替换
let odiv = document.createElement('div')
odiv.id = 'odiv'
odiv.style.width = '200px'
odiv.style.height = '200px'
odiv.style.background = 'pink'
document.body.appendChild(odiv)
let oh3 = document.createElement('h3')
oh3.id = 'oh3'
oh3.innerText = '我是h3'
odiv.parentNode.insertBefore(oh3,odiv)
odiv.parentNode.removeChild(odiv)
let clo = oh3.cloneNode(true)
clo.style.color = 'red'
document.body.appendChild(clo)
let oh1 = document.createElement('h1')
oh1.id = 'oh1'
oh1.innerText = '我是h1'
document.body.replaceChild(oh1,oh3)
DOM设置自定义属性
let odiv = document.createElement('div')
odiv.style.width = '200px'
odiv.style.height = '200px'
odiv.style.background = 'pink'
document.body.appendChild(odiv)
odiv.name = 'xiaohong'
odiv.setAttribute('name','xiaohong')
console.log(odiv.getAttribute('name'))
odiv.removeAttribute('name')