具有特定文本的jQuery选择器td

时间:2012-06-23 00:04:04

标签: jquery

我有一个jquery选择器,如下所示:

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

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

非常感谢您的帮助。

4 个答案:

答案 0 :(得分: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
});

答案 1 :(得分:7)

没有像这样的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
});

Try it out!

答案 2 :(得分:5)

裸选择器无法实现。

也许您可以使用.filter()之类的:

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

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

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

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

使用|| (或)代替&&找到带有文本另一个的TD。

答案 3 :(得分:0)

用文本过滤行TD

$("#table_id tr td").filter(
     function (index) { return $(this).text() == text_to_find; }
).parent().show()
相关问题