javascript removeChild“未找到节点”-无法解决
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8269590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
removeChild "Node was not found" - cant work it out
提问by Jason
Here is the code below I am using the remove cells (which have been selected) from a table. It works some of the time, but then other times it brings up a "Node was not found" code:
这是下面的代码,我正在使用表格中的删除单元格(已选择)。它有时会起作用,但有时会显示“未找到节点”代码:
"8" (NS_ERROR_DOM_NOT_FOUND_ERR)
“8”(NS_ERROR_DOM_NOT_FOUND_ERR)
Can't seem to figure how to do it. Any help would be great.
似乎无法弄清楚如何做到这一点。任何帮助都会很棒。
var p = document.getElementById('tableTr');
while(selectTag=document.getElementsByClassName('tagSelected')) {
if(!selectTag[0]) {
break;
}
if(selectTag[0].className=="tagSelected")
var c =selectTag[0];
p.removeChild(c);
}
}
I have a PHP script that populates the table and thats about it to my HTML:
我有一个填充表格的 PHP 脚本,这就是我的 HTML:
<div id="uploadTag">
<table class="tagBlock" id="tableTag" cellspacing="5px;">
<?php
$uploadlist=substr($uploadlists, 0, -1);
$uploadList=explode(";",$uploadlist);
$i=0;
foreach($uploadList as $key=>$list){
if($i==0)
{ ?>
<tr id="tableTr">
<?php }
$i++; $up="up".($key+1);
$imageext = substr(strrchr($list, '.'), 1);
$val=$list;
if ($imageext=='png' || $imageext=='bmp' || $imageext=='gif' || $imageext=='tif' || $imageext=='jpg')
{
$val="<image>";
}
?><td id="<?php echo $up; ?>" class="tagBlock" title="<?php echo $list; ?>"><div id="<?php echo $up; ?>" class="tagBlockW" title="<?php echo $list; ?>"><?php echo "$val"; ?></div></td> <?php
if($i==7){ $i=0;?> </tr> <?php }?>
<?php }
?>
</tr>
</table>
</div>
回答by RightSaidFred
There are a few issues here.
这里有几个问题。
- Do not use duplicate IDs.
- 不要使用重复的 ID。
It is not valid, and will give unexpected results. Either use a class, or make the IDs unique.
它无效,并且会产生意想不到的结果。要么使用类,要么使 ID 唯一。
- Do not do redundant DOM selection in a loop.
- 不要在循环中做多余的 DOM 选择。
DOM selection is an expensive operation. It should be done once, outside the loop, then cached.
DOM 选择是一项代价高昂的操作。它应该在循环外完成一次,然后缓存。
- There's no need to check for the
tagSelected
class inside the loop.
- 无需检查
tagSelected
循环内的类。
You used getElementsByClassName('tagSelected')
, so obviously they have that class.
你用过getElementsByClassName('tagSelected')
,所以很明显他们有那个类。
It seems that you simply want to remove the elements with the given class.
似乎您只是想删除具有给定类的元素。
// Fetch all elements in the document with the class 'tagSelected'
var selectTag = document.getElementsByClassName('tagSelected');
// Remove all of them.
while( selectTag[0] ) {
selectTag[0].parentNode.removeChild( selectTag[0] );
}
The reason that while( selectTag[0] ) {
works is that getElementsByClassName
returns a "live" NodeList. This means that when an element is removed from the DOM, the list is updated.
有效的原因while( selectTag[0] ) {
是getElementsByClassName
返回一个“实时”NodeList。这意味着当一个元素从 DOM 中移除时,列表会被更新。
So because we're removing all the elements, all we need to do is run the loop as long as there's something at index [0]
.
因为我们要删除所有元素,所以我们需要做的就是运行循环,只要 index 中有东西[0]
。
回答by Hardik Soni
var p = document.getElementById('tableTr');
while(selectTag=document.getElementsByClassName('tagSelected')) {
if(!selectTag[0]) {
break;
}
if(selectTag[0].className=="tagSelected")
var c =selectTag[0];
p.removeChild(c);
}
}
First of all u have one error(might be) which is u dont have opening brace "{" after secound if condition.
首先,您有一个错误(可能是),那就是您在第二个 if 条件之后没有左大括号“{”。
You can rather do this actuly.
你实际上可以这样做。
var p = document.getElementById('tableTr');
for(var i=0;i<document.getElementsByClassName('tagSelected')).length;i++)
{
var c = selectTag[i];
p.removeChild(c);
}