Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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 使用jquery从html中删除表行_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用jquery从html中删除表行

Javascript 使用jquery从html中删除表行,javascript,jquery,html,Javascript,Jquery,Html,我想使用jQuery从表中删除一行。以下是html: <tr> <td><center><a id="remove" href="#"><span class="glyphicon glyphicon-remove"></span></a></center></td> </tr> })) 当我点击链接时,什么也没发生。有人可以帮我吗?您需要将代码包装在$(文档)中。准

我想使用jQuery从表中删除一行。以下是html:

<tr>
    <td><center><a id="remove" href="#"><span class="glyphicon glyphicon-remove"></span></a></center></td>
</tr>
}))


当我点击链接时,什么也没发生。有人可以帮我吗?

您需要将代码包装在
$(文档)中。准备好调用或将其移动到页面末尾的结束正文元素之前(
)。如果您在文档的头部执行该代码,那么您是在针对尚不存在的元素运行它

另外,如果要删除父行,请使用
$(this).closest('tr')
而不是
$(this).parent()
,因为这将选择非标准的
元素

$(document).ready(function() {
  $('td a').on('click', function(e) {
    //delete code.
    e.preventDefault();
    $(this).closest('tr').remove();
  });
});

如果在该处理程序中使用console.log(this),您会看到什么?您的代码是否在document.ready()调用中?旁注,
已不存在。您是否已验证单击是否已注册?另外,如果您有多个“删除”链接,您应该使用
class=“remove”
而不是
id=“remove”
,因为页面上每个id应该只有一个。如果i console.log(这)控制台中没有任何事情。我的代码在$(文档)中。ready(function(){$('td a')。on('click',function(e){//delete code.e.preventDefault();$(this.closest('tr').remove();console.log(this);});
$(document).ready(function() {
  $('td a').on('click', function(e) {
    //delete code.
    e.preventDefault();
    $(this).closest('tr').remove();
  });
});