gpt4 book ai didi

javascript - 热用循环删除 :nth-child elements, 吗?

转载 作者:行者123 更新时间:2023-11-28 14:53:09 25 4
gpt4 key购买 nike

我有一个循环:

for (var i = 0; i < data; i++) {
$(".element:nth-child(i)").hide();
console.log($(".element:nth-child(i)"));
}

但是我的控制台出现错误“语法错误,无法识别的表达式::nth-child”,这是怎么回事?如何使用循环从页面中删除我的元素?

最佳答案

Jquery 将按字面解释:".element:nth-child(i)"

你应该像这样使用它:

for (var i = 1; i < data; i++) {
$(".element:nth-child("+ i +")").hide();
console.log( $(".element:nth-child("+ i +")") );
}

Because jQuery's implementation of :nth- selectors is strictly derived from the CSS specification, the value of n is "1-indexed", meaning that the counting starts at 1. For other selector expressions such as :eq() or :even jQuery follows JavaScript's "0-indexed" counting. Given a single containing two

  • s, $( "li:nth-child(1)" ) selects the first
  • while $( "li:eq(1)" ) selects the second.

  • 第 n 个元素文档:https://api.jquery.com/nth-child-selector/

    关于javascript - 热用循环删除 :nth-child elements, 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43838322/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com