开发者

Inserting an element

开发者 https://www.devze.com 2022-12-08 15:28 出处:网络
Is there a method in Javascript to insert an eleme开发者_C百科nt after the current node.I know there is a methodwhich inserts an element before the current node of the XML.But is there a method to Ins

Is there a method in Javascript to insert an eleme开发者_C百科nt after the current node.I know there is a method which inserts an element before the current node of the XML.But is there a method to Ins. after the current node?


Just get the next sibling of the current node and insert the new node before that node using insertBefore:

currentNode.parentNode.insertBefore(newNode, currentNode.nextSibling);

If nextSibling is null, insertBefore inserts the new node at the end of the node list.


There is no direct method to insert a node after a specific node but there is a workaround:

var parent = currentNode.parentNode;
if(currentNode.nextSibling != null)
    parent.insertBefore(newNode,currentNode.nextSibling)
else
    parent.appendChild(newNode);
0

精彩评论

暂无评论...
验证码 换一张
取 消