"Notes" the significance and application of JS Parentsnode,lastchild,appendchild,insertbefore,nextsibling

Source: Internet
Author: User

These few days reading read these several attributes make a few notes

ParentNode: As the name implies, it is the parent () that obtains an element that is equal to the JQ.

LastChild: Gets the last child element.

AppendChild: Adds a child element to the back of an element.

InsertBefore: Adds an element to the front of an element. This function has two parameters insertbefore (Newelem,targetelem), Newelem is the element to be inserted, Targetelem is relative to Newelem

element, Calling this method must be called with Targetelem's parent element, which is Targetelem.parentnode

NextSibling: Gets the next sibling element of an element.

About adding elements here's a question. Since there is insertbefore, then there must be InsertAfter, right? But after all this is JS is not JQ so everything is not so perfect

So if we don't, we'll build it!

First, a JS code

function InsertAfter (Newelem,targetelem) {   var parent = Targetelem.parentnode;   Gets the parent element of the target element   if (parent.lastchild = = Targetelem) {        parent.appendchild (newelem);    Else  {        parent.insertbefore (newelem,targetelem.nextsibling);   }}

What's the matter with the code?

First of all, this method has two parameters Newelem and Targetelem.

Newelem is the element you want to insert, and Targetelem is the reference to the new element you're inserting.

The meaning of this method is that you have to insert the newelem into the back of the Targetelem, but it is not easy to implement it, so write the judgment:

1. Gets the parent element of the Targetelem (using the parentnode method) and stores it in the variable parent

2. The parent element is judged if Targetelem is the last child of its parent element then its parent element directly calls the AppendChild method to insert Newelem into the last

3. If not, the parent invokes InsertBefore to insert Newelem into the front of the next-level sibling element of Targetelem

Finished ~ ~

"Notes" the significance and application of JS Parentsnode,lastchild,appendchild,insertbefore,nextsibling

Related Article

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.