我有3分div的:在jQuery中,如何测试一个元素是否是同一类的多个元素中的第一个?
<div class='squares' id='div1'></div>
<div class='squares' id='div2'></div>
<div class='squares' id='div3'></div>
使用jQuery,我想CSS属性(边框右)适用于所有div的,除了第一个。
我应该使用什么if语句?我想在if语句中使用该类(而不是id)。
非常感谢。
我有3分div的:在jQuery中,如何测试一个元素是否是同一类的多个元素中的第一个?
<div class='squares' id='div1'></div>
<div class='squares' id='div2'></div>
<div class='squares' id='div3'></div>
使用jQuery,我想CSS属性(边框右)适用于所有div的,除了第一个。
我应该使用什么if语句?我想在if语句中使用该类(而不是id)。
非常感谢。
您可以使用:not()
和:first
选择:
$('.squares:not(:first)').addClass('border')
或:
$('.squares:not(:first)').css('border-right', 'value')
使用.is()
在if
声明:该
if (!$(this).is(".squares:first()")) {
// not the first square
}
您好,感谢。我只是有一点语法问题,因为我使用'this'选择器。我如何结合':not(:first)'?它是'$(this +':not(:first)')'。这没有奏效。 THanks – eric01 2012-08-04 03:07:56
'this'是上下文,所以放在上下文的位置。 '$('selector',context)' – elclanrs 2012-08-04 03:08:28
@ eric01嗨,不客气,':not'不行,如果你想取消选择第一个元素并且你的元素是兄弟,你可以试试这个:'' if($(this).index()!= 0)' – undefined 2012-08-04 03:16:03