以下是我的代码
$(document).ready(function(){
$('#click').click(function(){
$('#table').append('<tr><td> </td></tr>');
})
$('#remove').click(function(){
$('#table').remove('<tr><td> </td></tr>');
})
})
Run Code Online (Sandbox Code Playgroud)
<table width="100%" border="1" cellspacing="0" cellpadding="0" id="table">
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<div id="click">click</div>
<div id="remove">remove</div>
Run Code Online (Sandbox Code Playgroud)
当我添加一行它很好,但我不知道如何删除表的最后一行.您还可以查看在线演示.
我怎样才能做到这一点?
另外,我想当用户也点击任何一行时它会自行删除.我试过了,但是有一个问题.如果您单击该行,它将自行删除,但是当您通过单击#click添加行时,该行不会通过单击来删除自身.
这是我的代码:
$(document).ready(function(){
$('#click').click(function(){
$('#table').append('<tr><td> </td></tr>');
})
$('#remove').click(function(){
$('#table tr:last').remove();
})
$('#table tr').click(function(){
$(this).remove();
});
})
Run Code Online (Sandbox Code Playgroud)
Sam*_*son 25
如果要删除最后一个表行#table,则需要使用选择器将其作为目标,然后对其进行调用$.remove():
$('#remove').on("click", function(){
$('#table tr:last').remove();
})
Run Code Online (Sandbox Code Playgroud)
您无法删除,您必须指定要删除的节点$('#table tr:first')并将其删除 remove()
$('#remove').click(function(){
$('#table tr:first').remove();
})
Run Code Online (Sandbox Code Playgroud)