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

How to dynamically add divs using JavaScript

PHPz
Release: 2018-10-10 17:40:47
forward
2458 people have browsed it

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>
Copy after login

For more related tutorials, please visit JavaScript video tutorial

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template