Why can I not remove a child element I've just found? NOT_FOUND_ERR

后端 未结 4 1603
滥情空心
滥情空心 2021-01-07 20:20

I\'m building a script which has to patch XML files, including replacing one list of elements with another. The following function applies a patch (involving a possibly empt

相关标签:
4条回答
  • 2021-01-07 20:29

    Building on the diagnosis by @Maurice and @fahd...

    Can't you just put a condition before

    parent.removeChild(node);
    

    such as

    if (parent.isSameNode(node.getParentNode()))
    

    Then it would only remove a direct child of the given parent.

    0 讨论(0)
  • 2021-01-07 20:32

    parent.removeChild(node) is throwing a NOT_FOUND_ERR because node is not a child of parent. I see that node comes from getElementsByTagName which might not be an immediate child of parent. It could be anywhere under parent.

    0 讨论(0)
  • 2021-01-07 20:45

    how about

    nodeToBeRemoved.getParentNode().removeChild(nodeToBeRemoved);
    
    0 讨论(0)
  • 2021-01-07 20:50

    This is because when you are doing parent.removeChild(node), parent is not necessarily the parent of the node because getElementsByTagName() is doing a recursive search.

    0 讨论(0)
提交回复
热议问题