java与jquery的选择器区别_关于jquery选择器中:first和:first-child和:first-of-type的区别及:nth-child()和:nth-of-type()的区别...

:first:选择第一个出现符合的元素

:first-child:选择限制条件中的第一个元素,并且必须和冒号前面的标签一致

:first-of-type:选择所有限制条件下的第一个冒号前面的标签元素,此标签可以不是第一个

测试:first代码:

1

2

3

Hello1

Hello2

Hello3

Hello4

Hello5

Hello6

$(function(){

$zzz=$("p:first").text();

alert($zzz);

});

其中结果为:

179eb022c42191f6396380220a806dbf.png

测试:first-child代码:

1

2

3

Hello1

Hello2

Hello3

Hello4

Hello5

Hello6

$(function(){

$zzz=$("p:first-child").text();

alert($zzz);

});

48304ba5e6f9fe08f3fa1abda7d326ab.png

其中结果为:

f6f6e3665dbe33ff30f46b660e830a25.png

测试:first-of-type代码:

1

2

3

Hello1

Hello2

Hello3

Hello4

Hello5

Hello6

$(function(){

$zzz=$("p:first-of-type").text();

alert($zzz);

});

其中结果为:

a962cf4d38af2ab019ab9c0b0eed0cdb.png

怎么样,很好理解吧,研究了好半天的呀!

下面是补充的:nth-child()和:nth-of-type()区别

:nth-child:是选择父元素下的第几个元素,不分标签类别,计数从1开始

:nth-of-type:是选择父元素下的同类型元素的第几个元素。区分标签类别,计数从1开始

测试:nth-child()代码:

1

2

3

Hello1

Hello2

Hello3

Hello4

Hello5

Hello6

$(function(){

$zzz=$("p:nth-child(3)").text();

alert($zzz);

});

其中结果为:

ed31da31e9961f143c4a9dc4d7c5e6ac.png

测试:nth-of-type()代码:

1

2

3

Hello1

Hello2

Hello3

Hello4

Hello5

Hello6

$(function(){

$zzz=$("p:nth-of-type(3)").text();

alert($zzz);

});

其中结果为:

cdfb825eaf21c06b1c89725d8e18ffa2.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值