The .first()
method was added in jQuery 1.4.
The :first
selector has been around since 1.0.
From the docs:
:first
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.
.first()
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…