2012-02-22 53 views

回答

1

选择器按预期工作。请记住,CSS :nth-child选择器是基于一个(它从一开始计数)。

4n th元素是DH,其确实具有类别1

您的选择:.articles article.1:nth-child(4n)

HTML:

<section class="articles">   .articles 
<article class="1 4">A</article>  :nth-child(1) 
<article class="1 4">B</article>  :nth-child(2) 
<article class="2 2">C</article>  :nth-child(3) 
<article class="1">D</article>   :nth-child(4) and :nth-child(4n) and .1 
<article class="1">E</article>   :nth-child(5) 
<article class="3 2">F</article>  :nth-child(6) 
<article class="1 4">G</article>  :nth-child(7) 
<article class="1 2">H</article>  :nth-child(8) and :nth-child(4n) and .1 
<article class="1 3 4">I</article>  :nth-child(9) 
<article class="1 2 4">J</article>  :nth-child(10) 
</section> 
+0

不,我不这么认为。比较:http://jsfiddle.net/s2LrG/。第n个子选择器被忽略。 – Dan 2012-02-22 13:33:39

+0

@Dan如何忽略':nth-​​child'选择器?元素D和H是关于其父母的第4n(4和8)个子元素,并且它们都具有类“1”。你是否把':n-child(4n)'与':n-child(4n-1)'混淆了:http://jsfiddle.net/s2LrG/1/? – 2012-02-22 13:36:39

+0

也许这个问题存在混淆......我试图用这个例子来做的只是给一班'1'的第n个孩子'结束'一类。所以只有文章A,B,D,E,G,H,I和J有'1'的类,所以我想把这个类应用到这个选择的每个第四项。 E和J(类别为“1”的第4和第8项)应该是以“结束”类结束的项目。 – Dan 2012-02-22 13:38:21

0

尝试Lettering.js http://letteringjs.com/

+0

这个问题的相关性是什么? – 2012-02-22 13:36:59

+0

这是一个jQuery插件来设计个性角色。 – Connor 2012-02-22 13:38:33

0

没有,罗布W是正确的。

//$('.articles article').removeClass('end'); 
$('.articles article.1:nth-child(4n)').addClass('end');​ 

使d和H目标,但如果你将其设置为

//$('.articles article').removeClass('end'); 
$('.articles article.1:nth-child(5n)').addClass('end');​ 

Ë和J成为众矢之的。

编辑:第4个孩子是d和H d等于4和H等于8

1 = A 
2 = B 
3 = C 
4 = D 

等,你得到你的号码混淆。 E和J分别是第5和第10个孩子。

+0

是的,我可以看到,这可能是应该的(我没有在jQuery中发现一个巨大的bug)),但我努力想要实现我实际想要做的事情。我基本上想要分两部分来做:首先,获得所有具有'1'类的文章。然后对该特定选择的每个第4项应用一个“结束”类。它现在正在做的是如果它有一个'1'级,那么接受第n个孩子并添加这个班级。我想要做的只是拿一类'1'的文章,然后对这个选择的每第n项应用一类'结束'。 – Dan 2012-02-22 13:45:36

+0

数字是正确的 - 完全可以。问题是,在我的例子中,'C'没有'1'类,所以它不应该(至少我不希望它)计入第n个子选择。我只想在最初的选择中包含具有'1'类的文章,然后对这些文章进行n次筛选。 – Dan 2012-02-22 13:52:47

相关问题