Creation of tags in JS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.