DOM的全称是(document object model)一共有12种节点类型,最常用的有:元素节点(nodeType:1),属性节点(nodeType:2),文本节点(nodeType:3),注释节点(nodeType:8)
nodeName:获取节点名称
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<div id="wrap"></div>
<script>
(function(){
let oW = document.getElementById("wrap");
console.log(oW.nodeName)//打印结果为:DIV
})()
</script>
</body>
</html>
chileNames:获取节点的集合
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<div id="wrap">
你好,世界
<p>hello world</p>
</div>
<script>
(function(){
let oW = document.getElementById("wrap");
console.log(oW.childNodes);
//这里就会获取到3个子节点:text,p,text
})()
</script>
</body>
</html>
注意:只要是空格都算是text节点
chlidren:获取元素子节点的集合(动态的)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<div id="wrap">
你好,世界
<p>hello world</p>
<span>ni hao</span>
</div>
<script>
(function(){
let oW = document.getElementById("wrap");
console.log(oW.children);
//这里就获取到了两个元素:p,span
})()
</script>
</body>
</html>
createTextNode:创建文本节点
let oText = document.createTextNode("hello world");
createElement:创建元素节点
let oDiv = document.createElement("div");//这里写标签名
appendChild:添加子元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<div id="wrap">
你好,世界
<p id="op">hello world</p>
<span>ni hao</span>
</div>
<script>
(function(){
let oWrap = document.getElementById("wrap");
let oText = document.createTextNode("hello world");
let oDiv = document.createElement("div");
oWrap.appendChild(oDiv);
oWrap.appendChild(oText);
console.log(oWrap);
})()
</script>
</body>
</html>
insertBefore:可在已有的子节点前插入一个新的子节点
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<div id="wrap">
你好,世界
<p id="op">hello world</p>
<span>ni hao</span>
</div>
<script>
(function(){
let oWrap = document.getElementById("wrap");
let oP = document.getElementById("op");
let oText = document.createTextNode("hello world");
let oDiv = document.createElement("div");
oWrap.insertBefore(oDiv,oP);//在p标签的前面添加div标签
oWrap.insertBefore(oText,oWrap.children[0]);//在id为wrap的div最前面添加文本节点
console.log(oWrap);
})()
</script>
</body>
</html>
removeChild:删除节点
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style></style>
</head>
<body>
<div id="wrap">
<p>111</p>
<p>222</p>
<p>333</p>
</div>
<script>
(function(){
let oWrap = document.getElementById("wrap");
let op = document.querySelectorAll("#wrap p");
oWrap.removeChild(op[1]);//222就已经删除了
})()
</script>
</body>
</html>
注意:removeChild删除的节点只要还在内存中就还是可以再次利用的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style></style>
</head>
<body>
<div id="wrap">
<p>111</p>
<p>222</p>
<p>333</p>
</div>
<div id="box"></div>
<script>
(function(){
let oWrap = document.getElementById("wrap"),
oBox = document.getElementById("box");
let op = document.querySelectorAll("#wrap p");
oWrap.removeChild(op[1]);//已经把id为wrap的div里的222p标签删除了
oBox.appendChild(op[1]);//添加到了id为box的div里了
})()
</script>
</body>
</html>
firstChild:第一个子节点(不支持IE8及以下)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="wrap">
<p>111</p>
<p>222</p>
</div>
<script>
(function () {
let oW = document.getElementById("wrap");
console.log(oW.firstChild);
})();
</script>
</body>
</html>
注意:这里的第一个子节点包括父级与子级中间的空格,所以这里打印的是:而不是111的p标签
firstElementChild:第一个元素子节点
(function () {
let oW = document.getElementById("wrap");
console.log(oW.firstElementChild);
})();
这里打印的才是111的p标签:
netElementSibling:下一个兄弟元素节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="wrap">
<p>111</p>
<p>222</p>
<p>333</p>
</div>
<script>
(function () {
let oW = document.getElementById("wrap").children;
console.log(oW[0].nextElementSibling);//222的p
console.log(oW[1].nextElementSibling);//333的p
console.log(oW[2].nextElementSibling);//当下一个子节点没有就返回null
})();
</script>
</body>
</html>
注意:netElementSibling不兼容IE8及一下:兼容代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="wrap">
<p>111</p>
<p>222</p>
<p>333</p>
</div>
<script>
(function () {
let oW = document.getElementById("wrap").children;
console.log(fn(oW[0]));//222的p
console.log(fn(oW[1]));//333的p
console.log(fn(oW[2]));返回null
function fn(ele) {
if (ele.nextElementSibling !== undefined){
return ele.nextElementSibling;
}else{
return ele.nextSibling;
}
}
})();
</script>
</body>
</html>
previousElementSibling:获取上一个兄弟元素节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="wrap">
<p>111</p>
<p>222</p>
<p>333</p>
</div>
<script>
(function () {
let oW = document.getElementById("wrap").children;
console.log(oW[0].previousElementSibling);//返回null
console.log(oW[1].previousElementSibling);//111的p
console.log(oW[2].previousElementSibling);//222的p
})();
</script>
</body>
</html>
parentDode:获取父级节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="wrap">
<p>111</p>
<p>222</p>
<p>333</p>
</div>
<script>
(function () {
let oW = document.getElementById("wrap").children;
console.log(oW[0].parentNode);//返回id为wrap的div
})();
</script>
</body>
</html>
offsetParent:获取定位父节点,就是有定位的父级元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="wrap">
<p id="btn">111</p>
<p>222</p>
<p>333</p>
</div>
<script>
(function () {
let oW = document.getElementById("wrap").children;
let oBtn = document.getElementById("btn");
console.log(oBtn.offsetParent);//返回body节点,因为他的父级div没有定位,所以最终获取body
})();
</script>
</body>
</html>
cloneNode:克隆节点(只是克隆一个空壳子,节点里面的内容是不会克隆过来的,要加个参数:true就行)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="wrap">
<p id="btn">111</p>
<p>222</p>
<p>333</p>
</div>
<div id="box">
</div>
<script>
(function () {
let oW = document.getElementById("wrap").children;
let oBtn = document.getElementById("btn");
let oBox = document.getElementById("box");
let clone = oBtn.cloneNode();//这里只能克隆一个空的id为btn的p
oBox.appendChild(clone);
let clone1 = oBtn.cloneNode(true);//这里把id为btn的p的内容都克隆过来了
oBox.appendChild(clone1);
})();
</script>
</body>
</html>