【问题标题】:Jquery: Doing a search on second column of a tableJquery:对表的第二列进行搜索
【发布时间】:2017-08-17 02:27:53
【问题描述】:

参考 this answer对表行进行实时搜索,答案说明了如何使用以下语句搜索第一列:

var id = $row.find("td:first").text();

我的要求是搜索第二列。用second 替换first 不起作用。

【问题讨论】:

  • 使用nth-child搜索css语法找到第二个TD。
  • @ReyanTropia 你的意思是nth-child(n) ?其中n 指的是列号?
  • 是的。或者你也可以使用下面的答案index 基本上eq(1) 会让你成为第二个孩子

标签: jquery search


【解决方案1】:

你可以使用eq选择器:

var id = $row.find("td:eq(1)").text();

索引从 0 开始。


您也可以改用 css 选择器:nth-child。 css 中没有 :second 或类似 :third 的选择器。 css 选择器只接受 :first-child, :last-child 选择器,而 jQuery 使用简写 :first:last 选择器来使用它们。所以,你可以在 jQuery 选择器中使用:first, :last, :first-child, :last-child

var id = $row.find("td:nth-child(2)").text();

:nth-child 索引从 1 开始。

【讨论】:

  • 测试了这两种方法。工作正常。如果您可以针对所引用的问题提供完整的代码,这将对所有人都有帮助。干杯!
【解决方案2】:

另一种搜索第二列单元格的方法是使用nth-of-type() CSS selector

$('td:nth-of-type(2)').each(function(index){
    $(this).text();
})

【讨论】:

  • :nth-of-type 用于不同的情况。
  • 我认为可以在这里使用“:nth-of-type(n) 选择器匹配其父元素的特定类型的第 n 个子元素。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
相关资源
最近更新 更多