码迷,mamicode.com
首页 > 编程语言 > 详细

javascript追加节点

时间:2015-09-06 09:41:49      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

追加节点

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
<style type="text/css">
li{border:1px solid red;}
div{border:5px solid green;}
</style>
</head>
<body>
<ul id="ul1"><li>li-1</li><li>li-2</li><li>li-3</li><li>li-4</li><li>li-5</li></ul>
<div id="div1">div-1</div>
<input type="button" value="给ul追加子li"/>
<input type="button" value="给div1追加哥哥"/>
<input type="button" value="把ul下li全部去掉"/>
</body>
</html>
<script type="text/javascript">
var i=0;
document.getElementsByTagName(‘input‘)[0].onclick=function(){
    i++;
    ob=document.createElement("li");
    ob.innerHTML="li-"+i;
    document.getElementById("ul1").appendChild(ob);
}
document.getElementsByTagName(‘input‘)[1].onclick=function(){
    divOb=document.getElementById("div1");
    ob1=document.createElement("div");
    ob1.innerHTML="div-n";
    document.body.insertBefore(ob1,divOb);
}
document.getElementsByTagName(‘input‘)[2].onclick=function(){
    var fOb=document.getElementById(‘ul1‘);
    var cOb=document.getElementById(‘ul1‘).childNodes;
    alert(cOb.length);
    for(var i=cOb.length-1;i>=0;i--){
        fOb.removeChild(cOb[i]);
    }
}

</script>

 

javascript追加节点

标签:

原文地址:http://www.cnblogs.com/wangjinke/p/4784441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!