jQuery nth-child选择器问题

时间:2016-09-15 16:39:09

标签: jquery jquery-selectors css-selectors

我需要使用jQuery隐藏html表的某些列。我正在使用以下代码:

$('#my-table tr td:nth-child(7),th:nth-child(7)').hide()

代码正在运行,它隐藏了表的列,但不尊重表id选择器,它正在对当前文档中的所有表应用更改。

为了让它按预期工作,我应该改变什么?

2 个答案:

答案 0 :(得分:2)

您需要为两个选择器指定annotation.canShowCallout = true ,否则id会隐藏您在代码中可能拥有的每个 th:nth-child(7)

th:nth-child(7)

您还可以使用find()方法

来简化此操作
$('#my-table tr td:nth-child(7), #my-table th:nth-child(7)').hide()

修改

正如@A. Wolff指出的那样,使用这个可以更加简化:

$('#my-table tr').find('td:nth-child(7), th:nth-child(7)').hide()

答案 1 :(得分:1)

您可以使用comma separated multiple selectors但它应该是完整的选择器。

$('#my-table tr td:nth-child(7),#my-table th:nth-child(7)')
带有多个内部元素选择器的

find() 方法

$('#my-table tr').find('td:nth-child(7),th:nth-child(7)')