Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery先更改每个。不(';类';)_Jquery_Css_Jquery Selectors_Unrecognized Selector_Livesearch - Fatal编程技术网

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

我试图在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).hasClass('word-'+word)){
      // transformation
      $('.word-'+word).first().css('color', 'red').addClass('readed');
    }
  });
};
问题是它捕捉到了单词的第一个出现,但没有找到下一个,而是停留在第一个。它不承认添加了“readed”类。我不知道这是一个.first()还是.not()问题或其他问题。

我发现了两个bug

  • $('.word-'+word).first()
    是具有
    .word-
    的第一个span
  • $(this).not('.readed')
    是一个对象,因此作为if语句的条件,它始终为true
这是工作代码:

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我找到了一个更简单的实现,并在我的帖子中添加了它:)