Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 这是从表体中删除表行的有效方法吗?_Javascript_Html_Dom - Fatal编程技术网

Javascript 这是从表体中删除表行的有效方法吗?

Javascript 这是从表体中删除表行的有效方法吗?,javascript,html,dom,Javascript,Html,Dom,我不熟悉编码,我想知道是否有更简单的方法来遍历DOM,获取我需要的元素,并在单击时删除一行。如果可能的话,我非常希望这些答案是用香草JavaScript完成的,因为我目前正试图真正理解这种语言的语法 <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://stack

我不熟悉编码,我想知道是否有更简单的方法来遍历DOM,获取我需要的元素,并在单击时删除一行。如果可能的话,我非常希望这些答案是用香草JavaScript完成的,因为我目前正试图真正理解这种语言的语法

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <title></title>
  </head>
  <body>
    <h1>Lets do this</h1>
    <table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody id="body1">
    <tr data-row="row" class="rows">
      <th scope="srow">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr data-row="row" class="rows">
      <th scope="row">2 </th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr data-row="row" class="rows">
      <th scope="row">3</th>
      <td>Larry</td>
      <td>the Bird</td>
    </tr>
  </tbody>
</table>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

<script type="text/javascript">


   let body = document.getElementById('body1')
   let trs = document.querySelectorAll('tr')
   for(let i = 0; i < trs.length; i++){
     trs[i].addEventListener('click', function(event){
       body.removeChild(trs[i])
     })
   }



</script>
</body>
</html>

让我们这样做
#
弗斯特
最后
手柄
1.
做记号
奥托
@mdo
2.
雅各布
桑顿
@肥
3.
拉里
鸟
让body=document.getElementById('body1')
设trs=document.querySelectorAll('tr')
for(设i=0;i
还有另一种方法,使用
.remove()
,它更简洁:

for(document.queryselectoral(“#body1 tr”)的常量tr){
tr.addEventListener('click',()=>tr.remove());
}
让我们这样做吧
#
弗斯特
最后
手柄
1.
做记号
奥托
@mdo
2.
雅各布
桑顿
@肥
3.
拉里
鸟

您可以使用事件委派来避免在每个
tr
上添加事件。例如:

let body=document.getElementById('body1');
body.addEventListener('click',evt=>{
if(evt.target.parentNode.tagName='TR'){
body.removeChild(evt.target.parentNode);
}
});
这就假设您的
td
s中没有任何子项,以后可能不会出现这种情况。如果您有孩子,您必须找到家长
tr
,在这种情况下,如果您必须循环,它可能没有那么有效。

u可以尝试此方法


您所做的是最简单的,唯一更容易阅读的可能是使用
forEach
循环,但本质上是相同的
let tableRef = document.getElementById('body1');
tableRef.addEventListener('click', function(event){
  event.target.parentNode.remove();
;})