Dom(方法篇)

理解:文档对象模型(Document Object Model)

目录

一,attributes

 二,nodeType

 三,nodeName

 四,nodeValue

 五 (一),childNodes

 五 (二),firstChild与lastChild

 六,parentNode

 七,previousSibling与nextSibling

 八,ownerDocument

 九,createElement()与createTextNode()

 十, appendChild()

 十一,insertBefore()与replaceChild()

 十二,removeChild()

注:本文只为方便本人记忆的总结如有建议敬请留言


一,attributes

理解:返回该元素节点的属性节点集合

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
		//attributes属性: 返回该元素节点的属性节点集合。
		console.log(box.attributes);
        console.log(box.attributes[0]);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
	</body>
</html>

 二,nodeType

理解:返回获得的值,1为元素节点,2为属性类型,3为文本节点

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
		//nodeType  //1为元素节点,2为属性类型,3为文本节点 
        console.log(box.nodeType);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
	</body>
</html>

 三,nodeName

理解:返回属性名称

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
		//nodeName  //属性名称 
        console.log(box.attributes["name"]);
        console.log(box.attributes["name"].nodeName);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
	</body>
</html>

 四,nodeValue

理解:返回属性值

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
		//nodeValue  //属性值 
        console.log(box.attributes["name"]);
        console.log(box.attributes["name"].nodeValue);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
	</body>
</html>

 五 (一),childNodes

理解:属性返回子节点集合(元素中的空格被视为文本,而文本被视为节点,可以使用 length 属性来确定子节点的数量)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
		//childNodes: 子节点集合
        console.log(box.childNodes);
        console.log(box.childNodes.length);
		console.log(box.childNodes[0]);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
	</body>
</html>

 五 (二),firstChild与lastChild

理解:firstChild=》用于获取当前元素节点的第一个子节点,相当于 childNodes[0];

           lastChild=》用于获取当前元素节点的最后一个子节点

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
		//firstChild 用于获取当前元素节点的第一个子节点,相当于 childNodes[0];
		//lastChild 用于获取当前元素节点的最后一个子节点
        console.log(box.childNodes);
        console.log(box.firstChild);
        console.log(box.lastChild);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3></div>
	</body>
</html>

 六,parentNode

理解:属性返回该节点的父节点

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
		//parentNode 属性返回该节点的父节点
        console.log(box.parentNode);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3></div>
	</body>
</html>

 七,previousSibling与nextSibling

理解:previousSibling=》属性返回该节点的前一个同级节点

           nextSibling=》属性返回该节点的后一个同级节点

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var h = document.querySelector("#h");
        var p = document.querySelector("#p");
		//previousSibling 属性返回该节点的前一个同级节点
        console.log(p.previousSibling);
        //nextSibling 属性返回该节点的后一个同级节点
        console.log(h.nextSibling);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3 id="h">这是一个盒子</h3><p id="p">这是盒子里的p</p>
        </div>
	</body>
</html>

 八,ownerDocument

理解:属性返回该节点的文档对象根节点

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
		var box = document.querySelector("#box");
		//nextSibling 属性返回该节点的文档对象根节点
        console.log(box.ownerDocument);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
	</body>
</html>

 九,createElement()与createTextNode()

理解:createElement()=》方法创建标签

           createTextNode()=》方法创建一个文本节点

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
		//createTextNode() 创建标签
		var s1 = document.createElement("span");
        console.log(s1);
        //createTextNode() 创建一个文本节点
		var txt1 = document.createTextNode("哈哈");
        console.log(txt1);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
	</body>
</html>

 十, appendChild()

理解:方法将一个新节点添加到某个节点的子节点列表的末尾上

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
		//createTextNode() 创建标签
		var s1 = document.createElement("span");
        //createTextNode() 创建一个文本节点
		var txt1 = document.createTextNode("哈哈");
        //appendChild()方法将一个新节点添加到某个节点的子节点列表的末尾上
        box.appendChild(s1);
        s1.appendChild(txt1);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
	</body>
</html>

 十一,insertBefore()与replaceChild()

理解:insertBefore​​​​​​​(newnode,existingnode)=》通过父节点调用,方法可以把节点插入到指定节点的前面(newnode:必需,需要插入的节点对象;existingnode:可选,在其之前插入新节点的子节点。如果未规定,则 insertBefore 方法会在结尾插入 newnode​​​​​​​);

           replaceChild(newnode,oldnode)=》​​​​​​​通过父节点调用,方法可以把节点替换成指定的节点(newnode:必需,需要插入的节点对象;必需,您希望删除的节点对象);

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
        <style>
        #box,#box2{
        width:200px;
        height:200px;
        background:pink;
        margin-top:10px;
        }
        </style>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
        var box2 = document.querySelector("#box2");
        //createTextNode() 创建一个文本节点
		var txt1 = document.createTextNode("哈哈");
		var txt2 = document.createTextNode("呵呵");
		var txt3 = document.createTextNode("嘿嘿");
        //appendChild()方法将一个新节点添加到某个节点的子节点列表的末尾上
        box.childNodes[0].parentNode.appendChild(txt1,box.childNodes[0]);
        box2.childNodes[0].parentNode.appendChild(txt2,box2.childNodes[0]);
      	// replaceChild()通过父节点调用,方法可以把节点替换成指定的节点 	
        box2.childNodes[0].parentNode.replaceChild(txt3,box2.childNodes[3]);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
        <div id="box2" class="obox2" title="盒子2" name="mybox2">
			<h3>这是一个盒子2</h3>
		</div>
	</body>
</html>

 十二,removeChild()

理解:removeChild(node)=》通过父节点调用,方法可以删除指定子节点(node:必需,您希望删除的节点对象

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
        <style>
        #box,#box2{
        width:200px;
        height:200px;
        background:pink;
        margin-top:10px;
        }
        </style>
		<script type="text/javascript">
        window.onload = function(){
        var box = document.querySelector("#box");
        var box2 = document.querySelector("#box2");
        //createTextNode() 创建一个文本节点
		var txt1 = document.createTextNode("哈哈");
		var txt2 = document.createTextNode("呵呵");
        //appendChild()方法将一个新节点添加到某个节点的子节点列表的末尾上
        box.childNodes[0].parentNode.appendChild(txt1,box.childNodes[0]);
        box2.childNodes[0].parentNode.appendChild(txt2,box2.childNodes[0]);
      	// removeChild()通通过父节点调用,方法可以删除指定子节点 	
        box2.childNodes[0].parentNode.removeChild(box2.childNodes[3]);
        }
        </script>
    </head>
    <body>
		<div id="box" class="obox" title="盒子" name="mybox">
			<h3>这是一个盒子</h3>
		</div>
        <div id="box2" class="obox2" title="盒子2" name="mybox2">
			<h3>这是一个盒子2</h3>
		</div>
	</body>
</html>

注:本文只为方便本人记忆的总结如有建议敬请留言

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值