2012-03-28 9 views
0

我使用的CMS不允许编辑HTML,我只能使用JavaScript和HTML来自定义它的外观。有一张有3张桌子的页面,我想从第二张桌子中删除(或隐藏)第二列。以下是HTML代码:使用jQuery删除特定表中的列

<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> 

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

+0

你可以使用jQuery吗? – 2012-03-28 11:58:46

+0

既然你已经用jquery标记了这个,看看这个问题:http://stackoverflow.com/questions/455958/hide-show-column-in-an-html-table – 2012-03-28 12:01:32

+0

我看过类似的问题,但我遇到的问题只是针对第二张表。我无法编辑HTML,所以我只能使用jQuery来定位它。如何选择第二个表中的第二列而不选择其他两个表? – 2012-03-28 12:06:08

回答

2

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

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

您可以使用.remove()不使用.css()摆脱我所看到的是,表没有ID的列

0

使用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(); 
0

的。所以@ blackpla9ue的答案是最合适的。但是,如果您需要删除具有ID的表格的第二行,请参阅下面的代码。

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