标签:fun append inter play script ref func 步骤 set
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>插入节点 insertBefore</title>
<style>
div#parent{
width:600px;
height:300px;
background: yellowgreen;
overflow-y: auto;
}
a{
display: inline-block;
margin-right:10px;
margin-bottom:10px;
cursor: pointer;
}
</style>
</head>
<body>
bbbbbbbbbbbbbbbbbbbbbbb<br/>
bbbbbbbbbbbbbbbbbbbbbbb<br/>
<div id="parent">
<div id="one">第一个节点</div>
<!-- -->
<div id="tow">第二个节点</div>
</div>
bbbbbbbbbbbbbbbbbbbbbbb<br/>
bbbbbbbbbbbbbbbbbbbbbbb<br/>
bbbbbbbbbbbbbbbbbbbbbbb<br/>
<a href="javascript:add()">增加a</a>
<script>
function add(){
var a=document.createElement("a");
a.title="html5";
var oimg=document.createElement(‘img‘);
oimg.src="img/tab_19.jpg";
a.appendChild(oimg);
var opar=document.getElementById("parent");
var tow=document.getElementById("tow");
opar.insertBefore(a,tow);//父.insertBefore(新,旧)
a.onclick=function(){
opar.removeChild(this);//父节点.removeChild(子节点)
}
}
</script>
</body>
</html>
标签:fun append inter play script ref func 步骤 set
原文地址:https://www.cnblogs.com/ZXH-null/p/13206866.html