Jquery先更改每个。不(';类';)
我试图在Jquery中进行有效的实时搜索,但没有成功捕获文本中的第一个单词。我把每个单词都用这样的跨距括起来:Jquery先更改每个。不(';类';),jquery,css,jquery-selectors,unrecognized-selector,livesearch,Jquery,Css,Jquery Selectors,Unrecognized Selector,Livesearch,我试图在Jquery中进行有效的实时搜索,但没有成功捕获文本中的第一个单词。我把每个单词都用这样的跨距括起来: <span class="word word-this" id="word-#" aria-hidden="true">this</span> var word = function(word) { $('span').each(function() { if ($(this).not('.readed') && $(this).h
<span class="word word-this" id="word-#" aria-hidden="true">this</span>
var word = function(word) {
$('span').each(function() {
if ($(this).not('.readed') && $(this).hasClass('word-'+word)){
// transformation
$('.word-'+word).first().css('color', 'red').addClass('readed');
}
});
};
问题是它捕捉到了单词的第一个出现,但没有找到下一个,而是停留在第一个。它不承认添加了“readed”类。我不知道这是一个.first()还是.not()问题或其他问题。我发现了两个bug
是具有$('.word-'+word).first()
的第一个span.word-
是一个对象,因此作为if语句的条件,它始终为true$(this).not('.readed')
var word = function(word) {
$('span').not('.readed').each(function() {
if ($(this).hasClass('word-'+word)) {
// transformation
$(this).css('color', 'red').addClass('readed');
// interrupt the each loop
return false;
}
});
};
我发现了一个更简单的实现
var word = function(word) {
$('span.word-'+word).not('.readed').first().each(function() {
$(this).css('color', 'red').addClass('readed');
});
};
当我这样做时,每一个有
.word-
变化的span请让我收回答案。对不起,它跑了!多谢各位@set0gut1@FlorianFromager我找到了一个更简单的实现,并在我的帖子中添加了它:)