繁体   English   中英

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

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

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

$(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM