繁体   English   中英

在jQuery中,如何测试某个元素是否是同一类的许多元素中的第一个?

[英]In jQuery, how to test whether an element is the first of many elements of same class?

我有3个div:

<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')

您是否使用:first:not()选择器绑定了?

我尚未测试,但是以下代码可以完成您想要的操作:

$(".squares:not(:first)").css("border-right", "thin solid red");

if语句中使用.is()

if (!$(this).is(".squares:first()")) {
    // not the first square 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM