js动态添加div(一)

来源:互联网 发布:如何ping网络端口号 编辑:程序博客网 时间:2024/06/11 04:21

利用JavaScript动态添加Div的方式有很多,一下是个比较常用的。

一、在一个Div前添加Div

<html>
  <body>
  <div   id="a">   
   <div   id="a1">1</div> 
   <div   id="a2">2</div>   
  </div>   
        <a href="javascript:addDiv();">test</a>                          
  </body>
<script   type="text/javascript">   
 function addDiv(){
        var   newNode=document.createElement("div");   
        newNode.setAttribute("id","a3");   
        var   txtNode=document.createTextNode("3");   
        newNode.appendChild(txtNode);   
         $("#a2").append(newNode);
      //  document.getElementById("a").insertBefore(newNode,document.getElementById("a2"));   
          
       // alert(document.getElementById("a").innerHTML)   
}
 </script>   
</html>

0 0