jquery第n个子选择器问题
我想在 div 中选择图像。我想要的图像编号为 2、5、8、11 等。
$('.thediv img:nth-child(3n+1)')..
不适合我,我错过了什么吗?谢谢
I want to select images in a div. The images i want are number 2,5,8,11 etc.
$('.thediv img:nth-child(3n+1)')..
Did not work for me, did i miss something? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的公式
3n + 1
不起作用,因为这些是将为以下n
值选择的元素:显然,这些不是第二个、第五个、第 8 个、第 11 个...选定的元素。它们中的每一个都相差 1。您需要使用公式
3n + 2
来代替,因此将选择这些元素:并且因为您在评论中说每个
img
位于a
中,:nth-child()
伪类应附加到a
,然后选择img :
Your formula
3n + 1
doesn't work because these are the elements that will be selected for the following values ofn
:Clearly, these aren't the 2nd, 5th, 8th, 11th ... elements selected. Each of them is off by 1. You need to use the formula
3n + 2
instead, so these elements will be selected:And since you said in your comment that each
img
is in ana
, the:nth-child()
pseudo-class should be attached toa
, then you select theimg
: