2013-11-01 21 views
1

我有了这个基本的html:如何调用表中所有tr的相同动作?

<table class="myClass"> 
    <tbody> 
     <tr><td>content</td></tr> 
     <tr><td>content</td></tr> 
     <tr><td>content</td></tr> 
     <tr><td>content</td></tr> 
    </tbody> 
</table> 

我想删除所有的“TR”的点击一个按钮。我试过以下,没有运气。这里有什么合适的语法?

$('.myClass tr').each().remove(); 
$('.myClass tbody tr').each().remove(); 
$('.myClass tbody').each().remove(tr); 
+0

http://jsfiddle.net/f5WQ7/1/ - >'$(“#按钮”)点击(函数()。{('。myClass tbody')。children()。remove(); });' –

回答

5

您只需拨打.remove()为TR设置

$('.myClass tr').remove(); 

您的代码失败,因为.each()需要一个函数作为参数,因为它不通过它将失败,错误Uncaught TypeError: Cannot call method 'call' of undefined所以其余的行动将不会执行

+0

@downvoter我是否错过任何东西 –

1

你不需要任何其他的东西。只需使用

$('.myClass tr').remove(); 
1

使用

$('tr').bind('click',function(){ 
    $(this).remove(); 
}); 

这将删除单击事件的每一行。但是,如果你想删除一个按钮的点击所有的行比你应该使用 -

$('.myClass tr').remove(); 
+0

OP想要删除所有tr元素tr点击了 –

+0

我不想删除tr点击。此外,我不是专家,但我相信绑定已弃用,应该使用'$(element).click(function(){...' –

相关问题