简体   繁体   English

帮助nth-child(),选择所有子项

[英]Help with nth-child(), selecting all children

I might have missed something completely stupid, but I can't figure this out. 我可能错过了一些完全愚蠢的东西,但我无法弄清楚。

I am working on some simple jquery practice and was looking to select the specified children from an aside element to get the width. 我正在进行一些简单的jquery练习,正在寻找从一个side元素中选择指定的孩子以获得宽度。 However, when I would select it, the return value was null . 但是,当我选择它时,返回值为null So I tried a few things, and I switch nth child value to 2 and the method to html() to see if that would do anything and yes infact, when I use nth-child(2) it selects all children in the aside element. 所以我尝试了几件事,然后将第n个子值切换为2,将方法切换为html()来查看是否可以做任何事情,是的,事实上,当我使用nth-child(2)时,它会选择aside元素中的所有子项。

Javascript: 使用Javascript:

var modOneWidth = $('aside:nth-child(1)').width();
console.log(modOneWidth); //returns null

var modOneWidth = $('aside:nth-child(2)').html();
console.log(modOneWidth); //returns html of all children in aside

Aside Element and it's children (which are placed in a <section> element): 除了Element及其子元素(放置在<section>元素中):

        <aside>
            <article>
                <h2>Module 1</h2>
                <p>ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
            </article>
            <article>
                <h2>Module 2</h2>
                <p>riosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur</p>
            </article>
            <article>
                <h2>Module 3</h2>
                <p>ipsum dolor sit arud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat.</p>
            </article>
        </aside>

Again, can't figure out what I'm doing wrong, can it be HTML5 elements? 再说一次,无法弄清楚我在做什么错,是HTML5元素吗? Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks a lot 非常感谢

You're applying the :nth-child() selector to the <aside> elements, so your selector ends up matching the <aside> element which is the nth child of its parent. 您将:nth-​​child()选择器应用于<aside>元素,因此选择器最终与作为其父级的第n个子元素的<aside>元素匹配。

You should match the <article> elements instead: 您应该匹配<article>元素:

var modOneWidth = $("aside article:nth-child(1)").width();

尝试

$('aside').children('article:eq(0)').html();

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

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