3

我有一个看起来像这样的 jquery 选择器:

$("#MasterListDVWP tr td:contains('" + currentSchool + "')").siblings("td:contains('Yes')").siblings("td:contains('CD'),td:contains('Other1')").each(function() {
// do something
});

有人可以帮我转换选择器,使其返回与确切文本匹配的对象,而不仅仅是包含吗?我知道这是无效的,但是像这样..siblings("td:equals('CD')")...

对你的帮助表示感谢。

4

4 回答 4

11

您最好的选择可能是 .filter() 函数。传递一个函数来测试你正在检查的元素的文本

$('#MasterListDVWP tr td').filter(function(){
  return $(this).text() === currentSchool;
}).siblings('td').filter(function(){
  return $(this).text() === 'CD' || $(this).text() === 'Other1';
}).each(function(){
  // do something
});
于 2012-06-23T00:11:04.790 回答
8

没有像这样的 jQuery 选择器,但您可以创建一个:

jQuery.extend(jQuery.expr[':'], {
    containsExactly: function (obj, index, meta, stack) {
        if ($(obj).text() === meta[3]) {
            return true;
        }
        return false;
    }
});

如果你这样做,那么你现在可以这样做:

$("#MasterListDVWP tr td:td:containsExactly('" + currentSchool + "')").siblings("td:containsExactly('Yes')").siblings("td:td:containsExactly('CD'),td:td:containsExactly('Other1')").each(function() {
// do something
});

试试看!

于 2012-06-23T00:13:23.023 回答
5

使用裸选择器是不可能的。

也许你可以.filter()像这样使用:

 $(...selectors...).filter(
     function (index) { return $(this).text() == "textToMatch"; }
);

对于同一元素的多个过滤:

 $(...selectors...).filter(
     function (index) { 
          return $(this).text() == "textToMatch" && $(this).text()=="anotherText"; 
     }
);

或者只是将各种.filter()声明链接在一起。

使用 || (或)而不是 && 来查找带有文本其他内容的 TD。

于 2012-06-23T00:07:18.637 回答
0

使用文本过滤行 TD

$("#table_id tr td").filter(
     function (index) { return $(this).text() == text_to_find; }
).parent().show()
于 2018-12-09T13:58:58.113 回答