jQuery :nth-child() selector

后端 未结 4 953
春和景丽
春和景丽 2020-12-16 05:18

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=\

相关标签:
4条回答
  • 2020-12-16 05:44

    From the docs (my emphasis)

    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");
    
    0 讨论(0)
  • 2020-12-16 05:45

    nth-child also seems to be non-0 indexed. Keep that in mind if you're used to indexing at 0.

    0 讨论(0)
  • 2020-12-16 05:49

    Try this selector:

    div.entry > div.box:nth-child(3n)
    
    0 讨论(0)
  • 2020-12-16 05:53

    Your :nth-child selector does not reference n, and you need to reference the inner div in your selector.

    Try:

    $(document).ready(function(){
      $("div.entry div:nth-child(3n)").css("margin", "0px");
    });
    
    0 讨论(0)
提交回复
热议问题