我有一个定义如下的表
<table>
<tr>
<td>
<input type='text' />
</td>
<td>
<button id ='first'>Button</button>
</td>
<td>
<input type='text' />
</td>
</tr>
$("#first").live('click',function(){
$(this).prev().remove();
$(this).remove();
});
如何删除td
中的前一个元素。但我不想删除td
发布于 2013-04-15 12:19:29
如果你想删除前一个TD的完整内容(而不是一个特定的输入元素),这是可行的:
$(document).on("click", "#first", function() {
$(this).closest("td").prev().empty();
});
发布于 2013-04-15 12:20:10
$(document).on("click", "#first", function() {
$(this).closest("td").prev().html("");
});
这将删除先前td
标记中的所有内容。
发布于 2013-04-15 12:14:35
您应该沿着DOM树向上直到父<td>
,然后获取前一个<td>
,在其中找到<input>
并将其删除。您应该使用带有事件委派的on()
,而不是过时的live()
$(document).on("click", "#first", function() {
$(this).closest("td").prev().find("input").remove();
});
请注意,您可以使用#first
的任何其他静态父元素来代替document
。
https://stackoverflow.com/questions/16014830
复制