li.touppercase():不是选择器困难



我有一些看起来像这样的代码:

$('.myList li:not(:contains(' + myWord + '))').css({'color' : red' });

被调用时,这将转动所有不包含我关键字红色的所有内容。

但是,这不是案例不敏感的搜索。为此,我尝试了这样的事情:

$('.myList li.'+toUpperCase()+':not(:contains(' + myWord.toUpperCase() + '))').css({'color' : 'red' });

,但这只是引发错误。

我该如何做一个:不(:包含情况不敏感的搜索?

我建议一种简单的方法:

$('.myList li').filter(function(){
    // I may have become lost in your approach, but the following
    // will filter the list to those nodes that *do not* contain
    // text equal to that contained in the myWord variable
    var text = $(this).text().toLowerCase();
    return text.indexOf(myWord.toLowerCase()) == -1;
}).css('color', red);

请注意:

$('.myList li.'+toUpperCase()+':not(:contains(' + myWord.toUpperCase() + '))').css({'color' : 'red' });

您似乎正在搜索具有等于upperCase()函数的类名的列表项(不会发生),然后您希望强制:contains()选择器对细菌敏感,您的've已经发现不是。

我建议您使用filter做到这一点,更简洁:

$('.myList li').filter(function() {
  return !new RegExp(myWord, 'i').test($(this).text());
}).css('color', 'red');

,如果您希望它对病例敏感。

,可以删除i标志。

最新更新