How to add a new element

Source: Internet
Author: User

There are two ways to make a new element in the DOM

Element.appendchild () Adds a new child node to the element as the last child node.

Node.appendchild (new Node object)
1. Create a new element createelement
2. Create a new text createTextNode
3. Combine new elements and new text to create a new label
4. Add the last child node to the node
Element.insertbefore () Inserts a new node before the specified existing child node.

Node.insertbefore (newnode new Node object, Existingnode)
A existingnode that represents the child node of the new node before it is inserted. If not specified, the InsertBefore method inserts newnode at the end.
1. Create a new element createelement
2. Create a new text createTextNode
3. Combine new elements and new text to create a new label
4. Get parent Node
5. Gets the node that will be added before.
6. Add a new node before the node
The difference between element.appendchild () and Element.insertbefore ():
The former is to add the last child node to the specified node, which is a parent-child relationship with the specified node, and the latter is a sibling to the pointing node before the node is specified.

How to add a new element

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.