原生js对dom点的操作

原生js对父节点,子节点的移除增加操作。共二级,贴出即可使用

<!DOCTYPE html>

<html>
<body>


<div id="div1">
<h1 id="p1">这是1个段落。</h1>
<p id="p2">
<h2 id="p21">这是21个段落。</h2>
<h3 id="p22">这是22个段落。</h3>
<h4 id="p23">这是23个段落。</h4>
</p>
<h5 id="p3">这是3个段落。</h5>
</div>
<button οnclick="removea()">移除段落1</button>
<button οnclick="removeb()">移除段落21</button>
<button οnclick="removec()">移除段落3</button>
<button οnclick="addd()">增加段落4</button>
<button οnclick="adde()">增加段落24</button>


<script>
var parent=document.getElementById("div1");
var child1=document.getElementById("p1");
var child2=document.getElementById("p2");
var child3=document.getElementById("p3");
var child2child1=document.getElementById("p21");
var child2child2=document.getElementById("p22");
var child2child3=document.getElementById("p23");


function removea(){
alert("移除段落1");
parent.removeChild(child1);


}
function removeb(){
alert("移除段落21");
parent.removeChild(child2child1);


}
function removec(){
alert("移除段落3");
parent.removeChild(child3);


}
function addd(){
var para=document.createElement("h1");
var node=document.createTextNode("这是第4个段落");
para.appendChild(node);


parent.appendChild(para);
}
function adde(){
var para=document.createElement("h3");
var node=document.createTextNode("这是第24个段落");
para.appendChild(node);


child2.appendChild(para);
}
</script>


</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值