JS Browser operation DOM Insert Dom

Source: Internet
Author: User

What should we do when we get a DOM node and want to insert a new DOM within this DOM node?

If the DOM node is empty, for example, <div></div> then the innerHTML = ‘<span>child</span>‘ content of the DOM node can be modified directly, equivalent to "inserting" a new DOM node.

If the DOM node is not empty, you cannot do so because innerHTML all of the original child nodes are replaced directly.

There are two ways to insert a new node. One is to use to appendChild add a child node to the last child node of the parent node. For example:

<!--HTML structure--><p id= "JS" >javascript</p><div id= "list" >    <p id= "java" >java</p >    <p id= "python" >Python</p>    <p id= "scheme" >Scheme</p></div>

To <p id="js">JavaScript</p> add to <div id="list"> the last item:

var = document.getElementById(' js '),    = document.getElementById (' list ');     List.appendchild (JS);

Now, the HTML structure has become this:

<!--HTML structure--><div id= "list" >    <p id= "java" >Java</p>    <p id= "python" >python </p>    <p id= "scheme" >Scheme</p>    <p id= "JS" >JavaScript</p></div>

JS Browser operation DOM Insert Dom

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.