1. 程式人生 > >js動態新增div(三)

js動態新增div(三)

三、動態新增Div,並刪除某個Div

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文件</title>
</head>
    <script type="text/javascript">
    var a=0;
    function add(){
        var o=document.getElementById("PhotoLabel");
        var div=document.createElement("div");
        div.id = div.name = "d"+ a; 
        div.innerHTML=o.innerHTML.replace(//{0/}/ig,a);
        document.getElementById("addPhotoLabel").appendChild(div);
        //document.write(document.getElementById("addPhotoLabel").innerHTML);
        alert(document.getElementById("addPhotoLabel").innerHTML);
        a++;
    }
    //window.onload =  function(){add();}
    function deleteDiv(){
     var oDiv= document.getElementById("d2");
      document.getElementById("addPhotoLabel").removeChild(oDiv);
    }
    </script>
<body>
<div id="PhotoLabel">
safasfdgdag
<a>aasscc</a>
</div>
<div id="addPhotoLabel"></div>
<a href="javascript:add();"><span style="font-size: 15px">增加</span></a>
<a href="javascript:deleteDiv();"><span style="font-size: 15px">刪除第二個</span></a>
</body>
</html>