jQuery difference between :eq() and :nth-child()

前端 未结 8 2360
一个人的身影
一个人的身影 2020-12-02 15:26

In jQuery, what are some of the key differences between using :eq() and :nth-child() to select any elements ?

Also in general, for the starting index, in which case

8条回答
  •  生来不讨喜
    2020-12-02 16:10

    :eq() allows you to access the elements in the jQuery object by index

    http://api.jquery.com/eq-selector/

    :nth-child also allows you to access the an element by index, however it only applies to the term to the immediate left of it.

    http://api.jquery.com/nth-child-selector/

    This means that if you want to pick one element from a selector then use :eq if you want to restrict selections to elements with n-1 preceding-sibilings then use nth-child.

    Javascript arrays are usually indexed from 0; however nth-child is indexed from 1 because it is technically a CSS property as opposed to a jQuery one.

提交回复
热议问题