jQuery冒号选择器 [英] jQuery colon selectors
问题描述
在jQuery中有一些冒号选择器,如
:prev,:next,,last
我的问题是:
- 他们真的是jQuery的一部分,因为他们实际上用于DOM元素?
- 我们似乎在jQuery中也有等效的方法
prev()
,next()
,last()
。有两种不同的方式的目的是什么?
任何基本的例子都是非常棒的。
解决方案jQuery没有
:prev
或:next
选择器,我不知道你在哪里遇到他们。虽然有一个:last
selector,以及:first
,由 Sizzle 选择库,由jQuery使用。它是非标准的选择器,不是CSS的一部分,因此可以在JavaScript中实现。
的一个目的是:最后一个
选择器在.last()
方法中,所以您可以使用它来过滤选择器序列中间的元素,如下所示(注意:last
和:last-child
不一样):$('。a> .b:last> .c')
而不必编写如下这样的方法链:
$('。a')。儿童( 'b ')的最后()的儿童(' C')。;
顺便说一下,你引用的冒号被称为伪类(俗语,但不正确)称为伪选择器)。
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
:prev
or:next
selectors, I have no idea where you came across them. There is a:last
selector, 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
:last
selector over the.last()
method is so you can use it to filter elements in the middle of a selector sequence, like this (note that:last
and:last-child
are not the same):$('.a > .b:last > .c')
Rather than having to write a chain of methods like this:
$('.a').children('.b').last().children('.c');
By the way, the "colon selectors" you refer to are called pseudo-classes (colloquially but incorrectly known as "pseudo-selectors").
这篇关于jQuery冒号选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!