Native js implements Append content to the end of a specified element _ javascript skills

Source: Internet
Author: User
Append some desired effects or attributes to the end of the specified element. You may have encountered this problem. Next, I will introduce the append implementation code in detail, if you are interested, refer The Code is as follows:


Var header1 = document. getElementById ("header ");
Var p = document. createElement ("p"); // create an element node
InsertAfter (p, header1); // create a method by yourself because js does not directly append the method to the end of the specified element.
Function insertAfter (newElement, targetElement) {// newElement is the element to be appended. targetElement is the position of the specified element.
Var parent = targetElement. parentNode; // locate the parent node of the specified Element
If (parent. lastChild = targetElement) {// determine whether the specified element is the last position in the node. if yes, use the appendChild method directly.
Parent. appendChild (newElement, targetElement );
} Else {
Parent. insertBefore (newElement, targetElement. nextSibling );
};
};

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.