jQuery 如何使用jquery删除td标签中的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16014830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:12:03  来源:igfitidea点击:

How to remove the content in the td tag using jquery

javascriptjquery

提问by Jonathan

I have a table defined like

我有一个表定义如下

<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();
});

How can i remove the previous element in the td. But i don't want to remove the td

如何删除td. 但我不想删除td

回答by Brian Stephens

If you want to remove the complete contents of that previous TD (rather than a specific INPUT element), this will work:

如果您想删除之前 TD 的完整内容(而不是特定的 INPUT 元素),这将起作用:

$(document).on("click", "#first", function() {
    $(this).closest("td").prev().empty();
});

回答by Miljan Puzovi?

http://jsfiddle.net/sfHBn/1/

http://jsfiddle.net/sfHBn/1/

$(document).on("click", "#first", function() {
    $(this).closest("td").prev().html("");
});

This will remove all content in previous tdtag.

这将删除前一个td标签中的所有内容。

回答by VisioN

You should go up the DOM tree until parent <td>, then get previous <td>, find <input>inside and remove it. You should use on()with event delegation instead of deprecated live():

您应该向上 DOM 树直到 parent <td>,然后获取 previous <td>,找到<input>里面并删除它。您应该使用on()with 事件委托而不是 deprecated live()

$(document).on("click", "#first", function() {
    $(this).closest("td").prev().find("input").remove();
});

Note, that instead of documentyou may use any other staticparent element of #first.

请注意,这不是document你可以使用任何其他静态的父元素#first

回答by Suresh Atta

Here is the one liner to achieve.

这是要实现的一个班轮。

     $(this).closest("td").prev().remove();

回答by Jai

I suggest you to use .on()instead because .live()is been now removed from the jQuery version 1.9+:

我建议你.on()改用它,因为.live()它现在已从 jQuery 1.9+ 版本中删除:

$(document).on('click', "#first", function(){
   $(this).closest('td').prev().children().remove();
});

Click the #firstbutton and get to the .closest()<td>and get the .prev()element which is a <td>as well then .remove()its .children().

单击#first按钮并到达.closest()<td>并获取.prev()元素,它既是 a<td>又是.remove()它的.children()