简体   繁体   English

第n个孩子选择了错误的元素

[英]nth-child selecting wrong element

For this particular site, when I utilize nth-child via CSS or jQuery, the 'nth-child' selector is capturing the wrong element. 对于这个特定的网站,当我通过CSS或jQuery使用nth-child时,“ nth-child”选择器捕获了错误的元素。 I'm getting one child before the selector that I'm calling: 我要叫的选择器前生了一个孩子:

.home article:nth-child(3) {} //captures 2nd child

This seems to be capturing the second child instead. 这似乎是在捕获第二个孩子。 If I attempt: 如果我尝试:

.home article:nth-child(1) {} //captures nothing

This captures no elements. 这不捕获任何元素。 In jQuery, it shows up as an empty array. 在jQuery中,它显示为一个空数组。 Here's the dev site that I'm working on. 这是我正在开发的开发站点。 Thank you. 谢谢。

http://heilbrice.designliftoff.com/ http://heilbrice.designliftoff.com/

In your site, you have a clearfix div that's the first child of its parent element within your container, so your first article is really the second child, not the first: 在您的站点中,您有一个clearfix div ,它是容器中其父元素的第一个孩子,因此您的第一article实际上是第二个孩子,而不是第一个孩子:

<div class="row-main clearfix">
    <div class="clearfix"></div>  <!-- .row-main.clearfix > :nth-child(1) -->

    <article id="post-" class=""> <!-- .row-main.clearfix > :nth-child(2) -->

In CSS, you can use :nth-of-type() instead to reach the third article element: 在CSS中,您可以使用:nth-of-type()代替第三个article元素:

/* Select the 3rd article in its parent within .home */
.home article:nth-of-type(3) {}

Oddly enough, jQuery does not support :nth-of-type() , so for a cross-browser solution you have to opt with :eq() with a zero-based index: 奇怪的是, jQuery不支持:nth-of-type() ,因此对于跨浏览器解决方案,您必须选择具有从零开始的索引的: :eq()

// Select the 3rd article within .home
$('.home article:eq(2)')

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

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