Failed to execute removeChild on Node

后端 未结 5 1766
盖世英雄少女心
盖世英雄少女心 2021-02-02 06:22

Other stack answers such as this and this seem to be specialized cases and I believe my case is more generalized. I am doing this in my js:

var markerDiv = docum         


        
5条回答
  •  渐次进展
    2021-02-02 06:59

    The direct parent of your child is markerDiv, so you should call remove from markerDiv as so:

    markerDiv.removeChild(myCoolDiv);
    

    Alternatively, you may want to remove markerNode. Since that node was appended directly to videoContainer, it can be removed with:

    document.getElementById("playerContainer").removeChild(markerDiv);
    

    Now, the easiest general way to remove a node, if you are absolutely confident that you did insert it into the DOM, is this:

    markerDiv.parentNode.removeChild(markerDiv);
    

    This works for any node (just replace markerDiv with a different node), and finds the parent of the node directly in order to call remove from it. If you are unsure if you added it, double check if the parentNode is non-null before calling removeChild.

提交回复
热议问题