【问题标题】:Remove an unclassified element jQuery删除一个未分类的元素jQuery
【发布时间】:2011-12-12 12:38:39
【问题描述】:

这是一个奇怪的问题,我正在尝试删除一个元素,但问题是,它是未分类的。完整的源代码在这里:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tbody>
  <tr> 
   <td valign="top"> 
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
     <tbody>
      <tr>          
       <td id="Header_ProductDetail_ProductDetails"></td>
       <td id="Header_ProductDetail_TechSpecs"></td>
       <td id="Header_ProductDetail_ExtInfo"></td>
      </tr>
     </tbody>
    </table>
   </td>
   <td width="1" valign="bottom">&nbsp;</td>
  </tr>
 </tbody>
</table>

你看到了吗

<td width="1" valign="bottom">&nbsp;</td>

我想删除它,但问题是我不想执行类似 td[valign=bottom].remove() 的操作,因为这意味着带有 valign="bottom" 的所有内容都将被删除。我宁愿去最近的元素,如 td#Header_ProductDetail_ProductDetails 添加类似 $('td#Header_ProductDetail_ProductDetails').parent().parent().parent().parent().closest(td).remove( ); 虽然,我知道这很疯狂,因为你可能无法跳出四个父母,但我不知道还有什么办法可以删除它?有谁知道这个问题有没有快速的解决办法?

【问题讨论】:

  • 什么指定您要删除该元素?是否要删除仅包含 &amp;nbsp; 的所有内容?带有width=1的元素?
  • 我只是想专门删除该元素,仅此而已。有没有办法找到tdwidth=1valign="bottom" 并包含&amp;nbsp; 有点像$('td[width="1"][valign="bottom"]').contains(&amp;nbsp;).remove();

标签: jquery html-table


【解决方案1】:

尝试使用过滤功能根据 html 内容过滤结果。

$("td").filter(function() { return $(this).html() === '&nbsp;'; }).remove();

【讨论】:

  • 类似:$('td[width="1"][valign="bottom"]').contains(&amp;nbsp;).remove(); 这可能吗?
  • @user1090389 - 我也有同样的想法,但 contains 不适用于像 &amp;nbsp; 这样的 html 内容
  • 我可以将td[valign=bottom][width=1] 添加到您的脚本中吗?
【解决方案2】:

根据您的结构的特殊性,您可以执行以下一些操作

$("td").eq(index).remove();  //Remove td of specific zero-based index
$("td").eq($("td").length-1).remove();  //Remove very last td

如果您提供了一些父元素类或 id,您可以非常具体地确定要选择的 td。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2013-09-23
    • 1970-01-01
    • 2012-10-04
    • 2018-08-30
    • 2010-12-11
    相关资源
    最近更新 更多