无法识别的表达式::nth-child

Unrecognized expression: :nth-child

我收到以下错误:

unrecognized expression: :nth-child

以下是与该问题相关的代码:

var path = $("#quickSearchContainer > ul > li:nth-child(i + 1)");

function resetSearch() {                      
  for (i = 0; i < SectorCheck.length; i++) {            
    console.log(path.text());
    console.log(SectorCheck[i]);
    if ((path.text()) === SectorCheck[i]) {
      path.hide()
    }   
  }
}

注意:如果我将 (i + 1) 更改为仅数字,它可以工作,但不是所需要的。

我怀疑您正试图用 i 索引到 path。如果是这样,就不会神奇地将 i 变量的值插入选择器,尤其是当它已经被执行时。

要索引到 path,请使用 [i](获取原始 DOM 元素)或 .eq(i)(获取围绕 jQuery 的包装器该位置的元素),请参阅评论:

var path = $("#quickSearchContainer > ul > li"); // Removed :nth-child(i + 1)

function resetSearch(){

    for (var i = 0; i < SectorCheck.length; i++) {  
    //   ^^^----- remember to declare your variables

        // Get the entry for `i` (as a jQuery object)
        var entry = path.eq(i);
        if (entry.text() === SectorCheck[i]) {
            entry.hide()
        }   
    }
}

另请注意,您需要声明i。您的代码因未声明而成为我所谓的 The Horror of Implicit Globals 的牺牲品。