jquery找到所有确切的td匹配 [英] jquery to find all exact td matches

查看:110
本文介绍了jquery找到所有确切的td匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('#servertable td:eq(' + server + ')')

这只找到1(我认为首先)匹配,如何找到所有匹配。
顺便说一句。 td:包含对我不起作用。

this finds only 1 (first I think) match, how to find all matches. btw. td:contains will not work for me.

推荐答案

eq 期待一个数字索引只返回一行。如果要根据内容匹配td,则必须使用:contains 选择器。说它不起作用并抛弃它不是解决问题的正确方法,因为选择器(很可能)没有错(请注意它的区分大小写,可能是它......)

eq expects a numerical index to only return a single row. If you want to match a td by its contents, you have to use the :contains selector. Saying "it doesn't work" and throwing it away is not the right approach to the problem, as the selector is (most likely) not at fault (Do note its case sensitive, which might be it...)

无论如何,如果您有这样的表:

Anyhow, if you have a table like this:

<table>
  <tr>
    <td>Hello</td>
    <td>World</td>
  </tr>
  <tr>
    <td>World</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
</table>

此jQuery代码:

$(function() {
    $("td:contains('Hello')").css('color','red');
});

将所有带Hello的单元格变为红色。 演示

Will turn all cells with "Hello" to red. Demo.

如果您需要不区分大小写的匹配项,你可以使用 过滤器 来做到这一点功能:

If you need a case insensitive match, you could do this, using the filter function:

$(function() {
    var search = 'HELLO'.toLowerCase();
    $("td").filter(function() {
        return $(this).text().toLowerCase().indexOf(search) != -1;
    }).css('color','red');
});   

如果您需要匹配单元格的完全内容,您可以使用类似于上面的东西:

If you need to match the exact contents of the cell, you could use something similar to the above:

$(function() {
    var search = 'HELLO'.toLowerCase();
    $("td").filter(function() {
        return $(this).text().toLowerCase() == search;
    }).css('color','red');
});

以上不区分大小写(通过在比较时将搜索和内容都转换为小写)否则如果你想要区分大小写,你可以删除它们。 演示

The above is case insensitive (by turning both the search and the contents to lower case when comparing) otherwise you can just remove those if you want case sensitivity. Demo.

这篇关于jquery找到所有确切的td匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆