jQuery onclick删除表格行

如何在点击时删除表格行?

这是一个jsfiddle 。

我想只删除嵌套del链接的行,而不是脚本现在执行的最后一行。

Onclick调用delTableRow()函数,并且需要更改该函数以删除嵌套的del链接行。

   function addTableRow(jQtable){ jQtable.each(function(){ var tds = ''; jQuery.each($('tr:last td', this), function() {tds += ''+$(this).html()+'';}); tds += ''; if($('tbody', this).length > 0){$('tbody', this).append(tds); }else {$(this).append(tds);} }); } function delTableRow(jQtable){ jQtable.each(function(){ $('tr:last', this).remove(); }); }  
11 12 13 del
21 22 23 del
31 32 33 del

删除onclick并替换为类(即class =“remove”)。 将事件绑定到表 – 这将使您获得比使用大量事件处理程序更高的性能,并确保添加的新行也将遵循此行为。

 $('table').on('click','tr a.remove',function(e){ e.preventDefault(); $(this).closest('tr').remove(); }); 

删除所有你的内联javascript并使用click事件….不需要在attr中调用onclick事件……这应该是他的技巧.. $(this).parents('tr').remove();

试试这个

    //---^---here remove the onclick inline function for all..  .... 
11 12 13 del

在这里工作小提琴

 $('td a').on('click',function(e){ //delete code. e.preventDefault(); $(this).parent().parent().remove(); // OR $(this).parents('tr').remove(); }); 
 $('td a').on('click',function(){ e.preventDefault(); $(this).parent().parent().remove(); }); 

使用

 $(this).parent().parent().remove(); 

在锚标签上。 因此,如果您的链接是td的子节点,它是tr的子节点,那么tr将被删除。