标签:pen zhang onclick set pre button width dom setattr
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function t1(){
var div1 = document.getElementById("div1");
var p = document.createElement("p");
var t = document.createTextNode("u34");
p.setAttribute("name","zhangsan");
p.appendChild(t);
div1.appendChild(p);
}
function t2(){
var div1 = document.getElementById("div1");
var list = div1.childNodes;
if(list.length==0){
alert("无法删除!");
return;
}
div1.removeChild(list[0]);
}
function t3(){
var div1 = document.getElementById("div1");
var list = div1.childNodes;
if(list.length==0){
alert("无法替换!");
return;
}
var h = document.createElement("h1");
var t = document.createTextNode("AAAA");
h.appendChild(t);
div1.replaceChild(h,list[list.length-1]);
}
function t4(){
var div1 = document.getElementById("div1");
var list = div1.childNodes;
var h = document.createElement("h1");
var t = document.createTextNode("BBBB");
h.appendChild(t);
div1.insertBefore(h,list[1]);
}
function t5(){
var div1 = document.getElementById("div1");
var h = document.createElement("h1");
var t = document.createTextNode("BBBB");
h.appendChild(t);
var hh = h.cloneNode(true);
div1.appendChild(hh);
}
</script>
</head>
<body>
<input type="button" onclick="t1()" value="增加"/>
<input type="button" onclick="t2()" value="删除"/>
<input type="button" onclick="t3()" value="替换"/>
<input type="button" onclick="t4()" value="插入"/>
<input type="button" onclick="t5()" value="复制插入"/>
<div id="div1" style="width: 300px;height: 300px;background-color: red;"></div>
</body>
</html>
标签:pen zhang onclick set pre button width dom setattr
原文地址:http://www.cnblogs.com/syj1993/p/6876491.html