问题
I have a UI, which when you press FIGHT it deletes the four currents divs FIGHT, STATUS, BLANK, BLANK2 and replaces them with new ones. This works except for the STATUS div. It brings up the error
"Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'."
and stops the other divs from deleting. When I take out the code removing the STATUS div parent0.removeChild(status);
the code works correctly with the obvious exception of the STATUS div deleting.
I am utterly confused since those four divs are nested within a div named UI, and I assume the error is referring to the fact that STATUS isn't a child of UI? That shouldn't be the case. Any help would be appreciated thanks.
JSfiddle:
https://jsfiddle.net/razorleaff/abks2dvq/3/
回答1:
Doing that way
parent0.removeChild(status);
is a bad practice, which causes problem like this, since status is a javascript reserve word instead of doing that just do
parent0.removeChild(document.getElementById('status'));
Definition and Usage The removeChild() method removes a specified child node of the specified element.
Returns the removed node as a Node object, or null if the node does not exist.
Parameter | Type | Description
node Node object
Required. The node object you want to remove
Updated Fiddle here
回答2:
The problem is window
object has a property named status and you can't access status element via id
. So, your code should be: (always use document.getElementById
to get an element using id
)
parent0.removeChild(document.getElementById('status'));
instead of:
parent0.removeChild(status);
来源:https://stackoverflow.com/questions/43317676/javascript-error-uncaught-typeerror-failed-to-execute-removechild-on-node