The .first()
method was added in jQuery 1.4.
The :first
selector has been around since 1.0.
From the docs:
The
:first
pseudo-class is equivalent to:eq(0)
. It could also be written as:lt(1)
. While this matches only a single element,:first-child
can match more than one: One for each parent.Given a jQuery object that represents a set of DOM elements, the
.first()
method constructs a new jQuery object from the first matching element.
It seems that .first()
is a filter that returns another jQuery object, while :first
is just a selector.
But, they can both be used to accomplish the same thing.
So, when should one be used instead of the other? Performance? Please provide examples.
See Question&Answers more detail:os