查找其文本不包含字符串的所有span元素

时间:2014-10-01 07:20:50

标签: jquery

我试图查找属于<span/>值不包含字符串一部分的特定类的所有text()元素。

这是我到目前为止所尝试的:

if ($("#details .error").not($(this).text().contains("This schedule is used")).length == 0) {
    //do something
}
else {
    //do something else   
}

但我收到Cannot read property '0' of undefined错误。 有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

试试这个:你不需要使用$(this),而是在选择器中使用contains()not()

if ($("#details .error:not(:contains(This schedule is used))").length == 0) {
    //do something
}
else {
    //do something else   
}

或者您可以使用filter()

执行相同操作
// get all span not having text specified
   var span = $("#details .error").filter(function(){
                  return $(this).text().indexOf('This schedule is used')==-1;
              });

   //check length of span
   if (span.length == 0) {
        //do something
    }
    else {
        //do something else   
    }

答案 1 :(得分:0)

if&#39;范围&#39;中没有this,因此请再次使用选择器

if ($("#details .error").not($("#details .error").text().contains("This schedule is used")).length == 0) {
    //do something
}
else {
    //do something else   
}
相关问题