【问题标题】:Remove column from specific table with JQuery使用 JQuery 从特定表中删除列
【发布时间】:2012-04-12 00:19:56
【问题描述】:

我使用的 CMS 不允许我编辑 HTML,我只能使用 JavaScript 和 HTML 来自定义它的外观。有一个包含 3 个表格的页面,我想从第二个表格中删除(或隐藏)第二列。这是 HTML 代码:

<TABLE>
   <TR>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
   </TR>
   <TR>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
   </TR>
</TABLE>

<TABLE>
   <TR>
      <TH>Row 1</TH>
      <TH>Row 2</TH>
      <TH>Row 3</TH>
   </TR>
   <TR>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
   </TR>
   <TR>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
   </TR>
</TABLE>

<TABLE>
   <TR>
      <TH>Row 1</TH>
      <TH>Row 2</TH>
      <TH>Row 3</TH>
   </TR>
   <TR>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
   </TR>
   <TR>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
      <TD>&nbsp;</TD>
   </TR>
</TABLE>

如何仅删除第二个表中的第二列?

【问题讨论】:

  • 既然你已经用jquery标记了这个,看看这个问题:stackoverflow.com/questions/455958/…
  • 我看过类似的问题,但我遇到的问题只是针对第二张桌子。我无法编辑 HTML,所以我只能使用 jQuery 来定位它。如何只选择第2个表中的第2列而不选择其他2个表?

标签: jquery


【解决方案1】:

检查这个小提琴:http://jsfiddle.net/FJfbW/1/

$('td:nth-child(2), th:nth-child(2)', 'table:eq(1) tr').css('background', '#f00');

你可以使用.remove()而不使用.css()来摆脱列

【讨论】:

    【解决方案2】:

    使用 jquery:http://jsfiddle.net/upeVs/

    $('table:eq(1) tr td:nth-child(2),table:eq(1) tr th:nth-child(2)').remove();
    

    编辑:
    @blackpla9ue 给出的解决方案可能性能更高一些,因为它只查找表行一次:

    $('td:nth-child(2), th:nth-child(2)', 'table:eq(1) tr').remove();
    

    【讨论】:

      【解决方案3】:

      我看到这些表没有 ID。所以@blackpla9ue 的回答是最合适的。但是,如果您需要删除具有 id 的表的第二行,请使用以下代码。

      $('td:nth-child(2), th:nth-child(2)', '#tblEventSearchResults tr').css('background', '#f00');​
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-07
        相关资源
        最近更新 更多