Home >Web Front-end >JS Tutorial >How to dynamically add divs using JavaScript

How to dynamically add divs using JavaScript

PHPz
PHPzforward
2016-05-16 19:25:212445browse

There are many ways to dynamically add p using JavaScript. The following is a more commonly used one.

1. Add p before a p

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

For more related tutorials, please visit JavaScript video tutorial

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete