In jQuery there are a few colon selectors like
:prev, :next, :last
My question is:
- Are they truly part of jQuery, because they are actually used on DOM elements?
- We seem to have equivalent methods as well in jQuery
prev(),next(),last(). What is the purpose of having 2 different ways?
Any basic examples would be really great.
jQuery does not have
:prevor:nextselectors, I have no idea where you came across them. There is a:lastselector, though, as well as:first, provided by the Sizzle selector library, used by jQuery. It is a non-standard selector, not part of CSS, and is thus implemented in JavaScript.One purpose of the
:lastselector over the.last()method is so you can use it to filter elements in the middle of a selector sequence, like this (note that:lastand:last-childare not the same):Rather than having to write a chain of methods like this:
By the way, the “colon selectors” you refer to are called pseudo-classes (colloquially but incorrectly known as “pseudo-selectors”).