Why does W3C Dom not provide insertafter? (Web) (JavaScript)

Source: Internet
Author: User

Abstract
W3C Dom defines insertbefore () and replaceChild (), why there is no insertafter ().

Introduction
How can I add traditional Chinese (Traditional Chinese) to my blog? (Web) (CSS) (JavaScript), I want to use insertafter (), but I found that W3C Dom did not set this function. Since it is not set, it must be said that the existing function can be completed. After a long time, Google will find the answer.

Insertafter () can be completed using insertbefore () and appendchild.

1 Function Insertafter (newelelement, targetelement ){
2 VaR Parent = Targetelement. parentnode;
3 If (Parent. lastchild = Targetelement ){
4 Parent. appendchild (newelement );
5 }
6 Else {
7 Parent. insertbefore (newelement, targetelement. nextsibling );
8 }
9 }
10

Generation has already explained everything. Simply put, judge whether the target element is the last node. If it is the last node, appendchild () is used to add it to the end, if it is not the last node, add insertbefore () to the front of the next node.

Conclusion
Maybe this insertafter () function can be stored in your library for later use.

See also
? (Web) (CSS) (JavaScript)
? (Web) (CSS) (JavaScript)

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.