JavaScript HTML DOM replaceChild替换子节点

目录

说明

语法:

1.用已有的节点去替换

2.用新创建的节点去替换


 

replaceChild方法用新节点(新创建的节点或者已经存在的节点)替换某个子节点。

语法:

 replaceChild(newnode,oldnode);
            newnode 新节点  oldnode 要替换的节点

1.用已有的节点去替换

 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=GBK">
        <style>
        *{
        	margin:5px;
        	padding:0;
        }
        #box{
        	border:1px solid blue;
        	width:300px;
        	height:200px;
        }
        #box1{
        	position:absolute;
        	left:50%;
        	top:0;
        	border:1px solid blue;
        	width:300px;
        	height:200px;
        }
        
        </style>
        </head>
        <body>
        	<div id="box"></div>
        	
        	<button onclick='add()'>添加元素</button>
        	<button onclick='do_replace()'>用第一个元素去替换box1的最后一个元素</button>
        	<div id="box1" >
        	</div>
        </body>
<script>
	var i=1;
	var box = document.getElementById("box");
	function add(){
		//创建元素box的子元素
		var p = document.createElement("p");
			p.innerText='这是box的P元素'+(i++);
			
			box.appendChild(p);
   }
   //默认给box1创建一个子元素,方便测试
   var box1 = document.getElementById("box1");
   var p1 = document.createElement("p");
       p1.innerText='这是box1的P元素';
	   box1.appendChild(p1);
		 
  function do_replace(){
  	if(!box.hasChildNodes()) return ;//如果没有子节点则返回
  	//用box的第一个元素去替换box1的最后一个元素
  	box1.replaceChild(box.firstChild,box1.lastChild);
  }

</script>
</html>

效果图:

2.用新创建的节点去替换

 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=GBK">
        <style>
        *{
        	margin:5px;
        	padding:0;
        }
        #box{
        	border:1px solid blue;
        	width:300px;
        	height:200px;
        }
        #box1{
        	position:absolute;
        	left:50%;
        	top:0;
        	border:1px solid blue;
        	width:300px;
        	height:200px;
        }
        
        </style>
        </head>
        <body>
        	<div id="box"></div>
        	
        	<button onclick='add()'>添加元素</button>
        	<button onclick='do_replace()'>用第一个元素去替换box1的最后一个元素</button>
        	<button onclick='do_newReplace()'>用新创建的元素替换box1的最后一个元素</button>
        	<div id="box1" >
        	</div>
        </body>
<script>
	var i=1;
	var box = document.getElementById("box");
	function add(){
		//创建元素box的子元素
		var p = document.createElement("p");
			p.innerText='这是box的P元素'+(i++);
			
			box.appendChild(p);
   }
   //默认给box1创建一个子元素,方便测试
   var box1 = document.getElementById("box1");
   var p1 = document.createElement("p");
       p1.innerText='这是box1的P元素';
	   box1.appendChild(p1);
		 
  function do_replace(){
  	if(!box.hasChildNodes()) return ;//如果没有子节点则返回
  	//用box的第一个元素去替换box1的最后一个元素
  	box1.replaceChild(box.firstChild,box1.lastChild);
  }
  var j=1;
  function do_newReplace(){
    var new_p = document.createElement("p");
        new_p.innerText='这是新创建的的P元素'+(j++);
  	//用新创建的元素替换box1的最后一个元素
  	box1.replaceChild(new_p,box1.lastChild);
  }

</script>
</html>

效果:

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程界小明哥

请博主喝瓶水,博主持续输出!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值