[英]How to remove the content in the td tag using jquery
我有一個表定義的
<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
如果要刪除前一個TD的完整內容(而不是特定的INPUT元素),這將起作用:
$(document).on("click", "#first", function() {
$(this).closest("td").prev().empty();
});
$(document).on("click", "#first", function() {
$(this).closest("td").prev().html("");
});
這將刪除先前td
標記中的所有內容。
你應該上DOM樹,直到父<td>
,然后得到前面的<td>
,在里面找到<input>
並刪除它。 你應該on()
使用事件委托而不是不贊成使用live()
:
$(document).on("click", "#first", function() {
$(this).closest("td").prev().find("input").remove();
});
請注意,您可以使用#first
任何其他靜態父元素代替document
。
這是實現的一個班輪。
$(this).closest("td").prev().remove();
我建議您使用.on()
因為.live()
現在已從jQuery版本1.9+中刪除:
$(document).on('click', "#first", function(){
$(this).closest('td').prev().children().remove();
});
單擊#first
按鈕並轉到.closest()
<td>
並獲取.prev()
元素,該元素是<td>
,然后.remove()
其.children()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.