Javascript error: Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

Javascript error: Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

我有一个 UI,当您按下 FIGHT 时,它会删除四个电流 divs FIGHT、STATUS、BLANK、BLANK2 并用新的替换它们。除了 STATUS div 之外,此方法有效。它提出了错误

"Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'."

并阻止其他 div 删除。当我取出删除 STATUS div parent0.removeChild(status); 的代码时,代码可以正常工作,但 STATUS div deleting.

明显例外。

我非常困惑,因为这四个 div 嵌套在一个名为 UI 的 div 中,我认为错误是指 STATUS 不是child 共 UI?不应该是这样的。如有任何帮助,我们将不胜感激。

JSfiddle:

https://jsfiddle.net/razorleaff/abks2dvq/3/

问题是 window 对象有一个名为 status 的 属性,您无法通过 id 访问状态元素。因此,您的代码应为:(始终使用 document.getElementById 以使用 id 获取元素)

parent0.removeChild(document.getElementById('status'));

而不是:

parent0.removeChild(status);

这样做

parent0.removeChild(status);

是一种不好的做法,会导致这样的问题, 因为 status 是一个 javascript 保留字而不是那样做

parent0.removeChild(document.getElementById('status'));

Definition and Usage The removeChild() method removes a specified child node of the specified element.

Returns 删除的节点作为 Node 对象,如果节点不存在则为 null。

 Parameter | Type | Description
  node        Node    object    

必填。您要移除的节点对象

Updated Fiddle here