jQuery:nth-​​child()选择器 [英] jQuery :nth-child() selector

查看:55
本文介绍了jQuery:nth-​​child()选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,请看下面的HTML.我试图使用jQuery来获取DIV中包含class="entry"的DIV中每个第三个实例,以使其没有右手边距:

Hi please look at the HTML below. I am trying to use jQuery to get every 3rd instance on the DIVs with class="box" contained within the DIV with class="entry" to have a no right hand margin:

我的HTML代码:

<div class="entry">

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box I Want to remove right hand margin on this div -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box I Want to remove right hand margin on this div -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box -->

    <div class="box">
        SOME HTML....
    </div><!-- end .box I Want to remove right hand margin on this div -->

    </div>
    <!--end entry-->

我尝试使用jQuery:

My attempt with jQuery:

   <script>
        $(document).ready(function(){
            $("div.entry:nth-child(3)").css("margin", "0px");
        });
   </script>

我无法正常工作,任何人都可以帮忙吗?预先感谢!

I can't get this working can anyone please help? Thanks in advance!

感谢所有提供所提供解决方案的人,这确实是正确的.我正在编码提供的模板,发现JQuery已设置为以兼容模式运行,因此$是问题所在.

thanks to all who helped the solution provided is indeed correct. I am coding up a supplied template and found that JQuery had been set to run in compatibility mode hence $ was the problem.

推荐答案

从文档中(我的重点)

匹配所有作为其父项的第n个子项或父项的偶数或奇数子项的元素.

Matches all elements that are the nth-child of their parent or that are the parent's even or odd children.

您当前正在选择父级,而应该选择子级:

You're currently selecting the parent, while you should be selecting children:

$("div.entry > div:nth-child(3)").css("margin", "0px");

这篇关于jQuery:nth-​​child()选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆