The javascript element creates the implementation method dynamically

  • 2020-06-12 08:25:19
  • OfStack

This article gives an example of how javascript elements are dynamically created. Share to everybody for everybody reference. The specific analysis is as follows:

document.write can only be created dynamically during page loading

You can call the createElement method of document to create an DOM object with the specified label, and then call the appendChild method of the element
The newly created element is added to the corresponding element

Examples are as follows:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Dom Dynamically creating elements </title>
 <script type="text/javascript">
 function CreateButton() {
  var div = document.getElementById("divMain");
  var myButton = document.createElement("input");
  myButton.type = "button";
  myButton.value = " I added it dynamically ";
  //myButton.id="btn";  Pay attention to : If set id Avoid repetition 
  div.appendChild(myButton); // Added to the div on 
 }
 </script>
</head>
<body>
 <div id="divMain"></div>
 <input type="button" value=" Add elements " onclick="CreateButton()" />
</body>
</html>

I hope this article has been helpful for your javascript programming.


Related articles: