jQuery nth-child nth-of-type Not Working [英] jQuery nth-child nth-of-type Not Working
问题描述
我需要在x个元素之后插入一个clearfix div,以便我可以获得格式良好的列。
I need to insert a clearfix div after x amount of elements so that I can get nicely formatted columns.
我已经尝试了两个 :nth-child
和:nth-of-type
,我只在前x个项目后添加了一个div。
I've tried both :nth-child
and :nth-of-type
and I only get a single div added after the first x items.
$('#content .product-layout:nth-child(3)').after('<div class="clearfix visible-lg"></div>');
在第三个.product-layout div之后创建一个div。
Creates a div after the third .product-layout div.
$('#product-row div:nth-child(3)').after('<div class="clearfix visible-lg"></div>');
在第三个.product-layout div之后创建一个div。
Creates a div after the third .product-layout div.
我需要在每个第3个现有产品布局div之后创建div。
I need to create the div after each 3rd existing product-layout div.
我到底做错了什么?
推荐答案
你缺少 n
来选择每个第三个子元素,类产品布局
#content
:
You're missing n
to select every third child element with class product-layout
of #content
:
$('#content .product-layout:nth-child(3n)').after('<div class="clearfix visible-lg"></div>');
// ---------------------------------- ^ here
这篇关于jQuery nth-child nth-of-type Not Working的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!