jQuery冒号选择器 [英] jQuery colon selectors

查看:133
本文介绍了jQuery冒号选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery中有一些冒号选择器,如


:prev,:next,,last


我的问题是:


  1. 他们真的是jQuery的一部分,因为他们实际上用于DOM元素?

  2. 我们似乎在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:

  1. Are they truly part of jQuery, because they are actually used on DOM elements?
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆