Javascript 错误:未捕获的类型错误:无法在“节点"上执行“removeChild":参数 1 的类型不是“节点" [英] Javascript error: Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

查看:37
本文介绍了Javascript 错误:未捕获的类型错误:无法在“节点"上执行“removeChild":参数 1 的类型不是“节点"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UI,当您按下 FIGHT 时,它会删除当前的四个 div FIGHT、STATUS、BLANK、BLANK2 并将它们替换为新的.这适用于 STATUS div.它带来了错误

<块引用>

未捕获的类型错误:无法在节点"上执行removeChild":参数 1 不是节点"类型."

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

我很困惑,因为这四个 div 嵌套在一个名为 UI 的 div 中,我认为错误是指 STATUS 不是 UI 的子级这一事实?不应该是这样.任何帮助将不胜感激谢谢.

JSfiddle:

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

解决方案

那样做

parent0.removeChild(status);

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

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

<块引用>

定义和用法removeChild() 方法移除指定元素的指定子节点.

将移除的节点作为 Node 对象返回,如果节点不存在,则返回 null.

 参数 |类型 |描述节点 节点对象

必填.要删除的节点对象

此处更新了小提琴

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/

解决方案

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

这篇关于Javascript 错误:未捕获的类型错误:无法在“节点"上执行“removeChild":参数 1 的类型不是“节点"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆