imj*_*uez 406
你是对的:
$('#myTableRow').remove();
Run Code Online (Sandbox Code Playgroud)
如果你的行有一个id,这可以正常工作,例如:
<tr id="myTableRow"><td>blah</td></tr>
Run Code Online (Sandbox Code Playgroud)
如果你没有id,你可以使用任何jQuery的众多选择器.
nic*_*ckf 98
$('#myTable tr').click(function(){
$(this).remove();
return false;
});
Run Code Online (Sandbox Code Playgroud)
slu*_*her 59
假设你的表格中有一个数据单元格内的按钮/链接,这样就可以了...
$(".delete").live('click', function(event) {
$(this).parent().parent().remove();
});
Run Code Online (Sandbox Code Playgroud)
这将删除单击的按钮/链接的父级的父级.你需要使用parent(),因为它是一个jQuery对象,而不是一个普通的DOM对象,你需要使用parent()两次,因为按钮位于一个数据单元内,它位于一行....这是你想要删除什么.$(this)是点击的按钮,所以简单地使用这样的东西只会删除按钮:
$(this).remove();
Run Code Online (Sandbox Code Playgroud)
虽然这将删除数据单元格:
$(this).parent().remove();
Run Code Online (Sandbox Code Playgroud)
如果你只想点击行上的任何地方删除它,这样的东西就行了.您可以轻松修改此选项以提示用户或仅在双击时工作:
$(".delete").live('click', function(event) {
$(this).parent().remove();
});
Run Code Online (Sandbox Code Playgroud)
希望有所帮助......我在这方面有点挣扎.
Ian*_*wis 42
您可以使用:
$($(this).closest("tr"))
Run Code Online (Sandbox Code Playgroud)
用于查找元素的父表行.
它比parent().parent()更优雅,这是我开始做的事情,很快就学会了我的方式的错误.
- 编辑 - 有人指出问题是关于删除行......
$($(this).closest("tr")).remove()
Run Code Online (Sandbox Code Playgroud)
如下所述,您可以简单地做到:
$(this).closest('tr').remove();
Run Code Online (Sandbox Code Playgroud)
类似的代码片段可用于许多操作,例如在多个元素上触发事件.
小智 15
容易..试试这个
$("table td img.delete").click(function () {
$(this).parent().parent().parent().fadeTo(400, 0, function () {
$(this).remove();
});
return false;
});
Run Code Online (Sandbox Code Playgroud)
function removeRow(row) {
$(row).remove();
}
<tr onmousedown="removeRow(this)"><td>Foo</td></tr>
Run Code Online (Sandbox Code Playgroud)
也许这样的事情也可以起作用?我没有尝试用"this"做某事,所以我不知道它是否有效.
您所要做的就是从表中删除表row(<tr>)标记.例如,以下是从表中删除最后一行的代码:
$('#myTable tr:last').remove();
*上面的代码来自这个jQuery Howto帖子.
尝试这个尺寸
$(this).parents('tr').first().remove();
Run Code Online (Sandbox Code Playgroud)
完整上市:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.deleteRowButton').click(DeleteRow);
});
function DeleteRow()
{
$(this).parents('tr').first().remove();
}
</script>
</head>
<body>
<table>
<tr><td>foo</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
<tr><td>bar bar</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
<tr><td>bazmati</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
490742 次 |
| 最近记录: |