The first method of creation (the page code will be overwritten after the page load is completed)
<! DOCTYPE html><div id= "DV" ></div><script> //document.write ("tag code and content");
my$ ("Btn"). Onclick=function () {
document.write ("<p> This is a p</p>");
};
my$ ("Btn"). Onclick=function () { document.write ("<p> This is a p</p>"), };//document.write ("<p > This is a p</p> "); document.write () create element, BUG: If you create an element this way when the page is loaded, all the content that exists on the page is wiped out </script></body>
The second way to create
<script> //Click the button to create a P tag in the DIV //The second way to create the element: object. innerhtml= "tag code and content"; my$ ("Btn"). Onclick=function () { my$ ("DV"). innerhtml= "<p> window before the moonlight, the suspicion is the ground frost, Jutou look at the moon, down thinking Hometown </p>"; }; </script>
The third way to create
The third way to create elements //create elements //document.createelement ("tag name"); object //Append element to Parent element //Click button to create a P my$ in Div ("Btn"). onclick = function () { //Create element var pObj = document.createelement ("P"); Setinnnertext (POBJ, "This is a P"); Appends the created child elements to the parent element my$ ("DV"). AppendChild (POBJ); }; Sets the middle text content of any element function Setinnnertext (element,text) {if (typeof element.textcontent== "undefined") { Element.innertext=text; }else{Element.textcontent=text; } }
Creation of tags in JS