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)