嗨,请看下面的HTML.我正在尝试使用jQuery来获取DIV上的每个第3个实例,其中class =“Box”包含在DIV中,并且class =“entry”没有右边距:
我的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的尝试:
<script> $(document).ready(function(){ $("div.entry:nth-child(3)").css("margin","0px"); }); </script>
我不能让这个工作有人可以帮忙吗?提前致谢!
感谢所有帮助解决方案的人确实是正确的.我正在编写一个提供的模板,发现JQuery已设置为以兼容模式运行,因此$是问题所在.
解决方法
来自文档(我的重点)
Matches all elements that are the nth-child of their parent or that are the parent’s even or odd children.
您当前正在选择父级,而您应该选择子级:
$("div.entry > div:nth-child(3)").css("margin","0px");