1

我正在尝试对 ID 不是“第一”的所有 div 采取行动。但不知何故,当我运行每个循环时,它会忽略第一个 div 并继续下一个。我有一种感觉,这很容易解决......

jQuery:

$(document).ready(function(){
    $.each($(".storyBlock:not(:first)"), function(i, object){
        console.log(i); //returns 0 for the second item, 1 for the third item
    });
});

HTML:

<div class="content">
    <div class="storyBlock" id="second" style="margin-top:1500px" img="mypathhere"></div>
    <div class="storyBlock" id="third" style="margin-top:1500px" img="mypathhere"></div>
    <div class="storyBlock" id="fourth" style="margin-top:1500px" img="mypathhere"></div>
</div>
4

6 回答 6

3

:first不同于#first. 前者是一组元素中的第一个元素,#first是ID属性为 的元素first,看起来就是你想要的。

请注意,您也可以将其写为:

$(".storyBlock:not(#first)").each
于 2013-06-04T16:16:41.560 回答
2

好吧,您将第一个项目作为 storyBlock 类的目标(这就是它:first所做的),所以它不是一个错误,而是一个特性。

试试这个 :

$(document).ready(function()
{
    $.each($(".storyBlock:not(#first)"), function(i, object){

        console.log(i);//returns 0 for the second item, 1 for the third item

    });
});
于 2013-06-04T16:16:12.163 回答
2

您也可以.not()为此使用遍历 API...

http://api.jquery.com/not/

(".storyBlock").not("#first").each(function(i, object){
    console.log(i);//returns 0 for the second item, 1 for the third item
});

First是一个选择器,它会从字面上为您提供匹配元素的第一个元素。

于 2013-06-04T16:18:19.360 回答
1

第一个 ID 有一个#first选择器

所以$(".storyBlock:not(#first)")应该这样做。

于 2013-06-04T16:16:00.990 回答
0

你也可以这样做:

$('.storyBlock:not(:first)').each(function() {})
于 2013-06-04T16:17:59.947 回答
0

试试这个

$(document).ready(function()
{
    $.each($(".storyBlock:not([id='first'])"), function(i, object){

        console.log(i);//returns 0 for the second item, 1 for the third item

    });
});
于 2013-06-04T16:18:55.447 回答