Javascript Can';I don’我不能让孩子正常工作

Javascript Can';I don’我不能让孩子正常工作,javascript,html,Javascript,Html,请原谅这个原始的问题。我想使用removeChild动态删除HTML表中的一行。我遵循了教程,但仍然没有与我一起工作。这是我正在做的一个例子。我得到的错误是: NotFoundError: Node was not found 代码和脚本: var currentRow=document.getElementById(“第2行”); var table=document.getElementById(“数据表”); 表.removeChild(当前行) A. B C A1 地下一层 C1

请原谅这个原始的问题。我想使用removeChild动态删除HTML表中的一行。我遵循了教程,但仍然没有与我一起工作。这是我正在做的一个例子。我得到的错误是:

NotFoundError: Node was not found
代码和脚本:

var currentRow=document.getElementById(“第2行”);
var table=document.getElementById(“数据表”);
表.removeChild(当前行)

A.
B
C
A1
地下一层
C1
A2
地下二层
C2

现代浏览器将
tr
包装在
tbody
元素中,因此您的代码不起作用

您可以使用获取对
tbody
的引用,然后将其删除

var currentRow=document.getElementById(“第2行”);
currentRow.parentNode.removeChild(currentRow)

A.
B
C
A1
地下一层
C1
A2
地下二层
C2

表行不是
元素的子元素。即使标记中没有
,DOM的构建也如同有:

<table>
  <tbody>
    <tr>
要删除
someElement
。就你而言:

currentRow = document.getElementById("row-2");
currentRow.parent.removeChild(currentRow);

请注意,除了
,还有
表格部分可用。但是,唯一隐式添加的是

问题是,即使您的代码有:

<table align="center" cellspacing=1 cellpadding=1 id="data-table" border=1 class="data-table">
  <tr id="head" class="head">
    <td class="head">A</td>
    <td class="head">B</td>
    <td class="head">C</td>
  </tr>

  <tr id="row-1" class="head">
    <td class="head" id="col1-1">A1</td>
    <td class="head" id="col2-1">B1</td>
    <td class="head" id="col3-1">C1</td>
  </tr>

  <tr id="row-2" class="head">
    <td class="head" id="col1-2">A2</td>
    <td class="head" id="col2-2">B2</td>
    <td class="head" id="col3-2">C2</td>
  </tr>

</table>
<script>
  var currentRow=document.getElementById("row-2");
  var table = document.getElementById("data-table");
  table.removeChild(currentRow);
</script>

A.
B
C
A1
地下一层
C1
A2
地下二层
C2
var currentRow=document.getElementById(“第2行”);
var table=document.getElementById(“数据表”);
表.removeChild(当前行);
在真实渲染中,它通过
进行变换。使用检查元件进行检查。通过简单的方式,您可以执行以下操作:

<table align="center" cellspacing=1 cellpadding=1 id="data-table" border=1 class="data-table">
  <tr id="head" class="head">
    <td class="head">A</td>
    <td class="head">B</td>
    <td class="head">C</td>
  </tr>

  <tr id="row-1" class="head">
    <td class="head" id="col1-1">A1</td>
    <td class="head" id="col2-1">B1</td>
    <td class="head" id="col3-1">C1</td>
  </tr>

  <tr id="row-2" class="head">
    <td class="head" id="col1-2">A2</td>
    <td class="head" id="col2-2">B2</td>
    <td class="head" id="col3-2">C2</td>
  </tr>

</table>
<script>
  var currentRow = document.getElementById("row-2");
  currentRow.parentNode.removeChild(currentRow);
</script>

A.
B
C
A1
地下一层
C1
A2
地下二层
C2
var currentRow=document.getElementById(“第2行”);
currentRow.parentNode.removeChild(currentRow);

A.
B
C
A1
地下一层
C1
A2
地下二层
C2
var currentRow=document.getElementById(“第2行”);
currentRow.parentNode.removeChild(currentRow);

你必须
从父母那里移除孩子

幸运的是,每个元素上都有对父元素的引用,因此您可以使用:

var currentRow = document.getElementById("row-2");
currentRow.parentNode.removeChild(currentRow);

这样可以节省一些额外的代码,并且易于阅读。

始终使用调试,如
console.log(table.children)
firstUse
currentRow.parentNode.removeChild(currentRow)相反。哇。你什么时候回答的
:O
虽然这是对问题的一个很好的解释,但通过添加JavaScript(实际上会删除所需的行)可以改进这个答案。
var currentRow = document.getElementById("row-2");
currentRow.parentNode.removeChild(currentRow);