前端天空

致力于前端开发应用

jquery之css选择器

08月 17th, 2010

使用过jquery的人都知道,jquery的选择器是很强大的,特别是对CSS选择器的支持

这里的地址是测试:http://www.tiscs.com/wyx/slickspeed/index.html#

所以想用好jquery的话,还是要先权威的了解一下W3C的css1—css3,

http://www.w3.org/TR/css3-selectors/

本节非规范性,因为它只是概括了以下几个部分。

甲选择是一个结构。这种结构可以作为一个条件(例如,在一个正确的CSS ) ,文档树中匹配一个元素的CSS,或作为一个单位说明HTML或XML片段相应的结构。

选拔范围可以从简单的元素名称,以丰富的内容表述。

下表总结了选择语法:

样本 解释 描述 CSS级别
* 匹配任意元素 Universal selector 2
E 匹配类型为E的元素 Type selector 1
E[foo] 匹配含有foo属性的E元素 Attribute selectors 2
E[foo="bar"] 匹配含有foo属性等于bar的E元素 Attribute selectors 2
E[foo~="bar"] 匹配含有foo属性中包含bar的E元素 Attribute selectors 2
E[foo^="bar"] 匹配含有foo属性的值从bar开头的E元素 Attribute selectors 3
E[foo$="bar"] 匹配含有foo属性的值以bar结尾的E元素 Attribute selectors 3
E[foo*="bar"] 匹配含有foo属性的值包含bar的E元素 Attribute selectors 3
E[hreflang|="en"] an E element whose “hreflang” attribute has a hyphen-separated list of values beginning (from the left) with “en” Attribute selectors 2
E:root an E element, root of the document Structural pseudo-classes 3
E:nth-child(n) an E element, the n-th child of its parent Structural pseudo-classes 3
E:nth-last-child(n) an E element, the n-th child of its parent, counting from the last one Structural pseudo-classes 3
E:nth-of-type(n) an E element, the n-th sibling of its type Structural pseudo-classes 3
E:nth-last-of-type(n) an E element, the n-th sibling of its type, counting from the last one Structural pseudo-classes 3
E:first-child an E element, first child of its parent Structural pseudo-classes 2
E:last-child an E element, last child of its parent Structural pseudo-classes 3
E:first-of-type an E element, first sibling of its type Structural pseudo-classes 3
E:last-of-type an E element, last sibling of its type Structural pseudo-classes 3
E:only-child an E element, only child of its parent Structural pseudo-classes 3
E:only-of-type an E element, only sibling of its type Structural pseudo-classes 3
E:empty an E element that has no children (including text nodes) Structural pseudo-classes 3
E:link
E:visited
an E element being the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited) The link pseudo-classes 1
E:active
E:hover
E:focus
an E element during certain user actions The user action pseudo-classes 1 and 2
E:target an E element being the target of the referring URI The target pseudo-class 3
E:lang(fr) an element of type E in language “fr” (the document language specifies how language is determined) The :lang() pseudo-class 2
E:enabled
E:disabled
a user interface element E which is enabled or disabled The UI element states pseudo-classes 3
E:checked a user interface element E which is checked (for instance a radio-button or checkbox) The UI element states pseudo-classes 3
E::first-line the first formatted line of an E element The ::first-line pseudo-element 1
E::first-letter the first formatted letter of an E element The ::first-letter pseudo-element 1
E::selection the portion of an E element that is currently selected/highlighted by the user The UI element fragments pseudo-elements 3
E::before generated content before an E element The ::before pseudo-element 2
E::after generated content after an E element The ::after pseudo-element 2
E.warning an E element whose class is “warning” (the document language specifies how class is determined). Class selectors 1
E#myid an E element with ID equal to “myid”. ID selectors 1
E:not(s) an E element that does not match simple selector s Negation pseudo-class 3
E F E元素下的所有F元素 Descendant combinator 1
E > F 所有E与F是父子关系的F元素 Child combinator 2
E + F
匹配所有紧接在 E元素后的 F元素
Adjacent sibling combinator 2
E ~ F
匹配 E 元素之后的所有 F元素
General sibling combinator 3

jQuery的:nth-child(index/even/odd/equation)

匹配其父元素下的第N个子或奇偶元素,’:eq(index)’ 只匹配一个元素,而这个将为每一个父元素匹配子元素。:nth-child从1开始的,而:eq()是从0算起的!

可以使用:

nth-child(even)
:nth-child(odd)
:nth-child(3n)
:nth-child(2)
:nth-child(3n+1)
:nth-child(3n+2)

例如:

在每个 ul 查找第 2 个li

HTML 代码:

<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul>

jQuery 代码:

$(“ul li:nth-child(2)”)

结果:

[ <li>Karl</li>, <li>Tane</li> ]

:nth-child(Xn+Y)

匹配集合中子集每隔X个元素的第Xn+Y元素集合。返回匹配元素集合

:nth-child(2n+3) 表示从第3个开始,递增2。结果:3、5、7、9 、11、…
:nth-child(3n+10) 表示从第10个开始,递增3。结果:10、13、16、19 、…

—-以下来自互联网

语法:

:nth-child(an+b)

为什么选择她,因为我认为,这个选择器是最多学问的一个了。很可惜,据我所测,目前能较好地支持她的只有Opera9+和Safari3+。

描述:

伪类:nth-child()的参数是an+b,如果按照w3.org上的描述,写成中文,很可能会让人头晕,再加上笔者的文笔水平有限,所以我决定避开an+b的说法,把它拆分成5种写法共5部分来说明。

第一种:简单数字序号写法

:nth-child(number)

直接匹配第number个元素。参数number必须为大于0的整数。

例子:

li:nth-child(3){background:orange;}/*把第3个LI的背景设为橙色*/

第二种:倍数写法

:nth-child(an)

匹配所有倍数为a的元素。其中参数an中的字母n不可缺省,它是倍数写法的标志,如3n、5n。

例子:

li:nth-child(3n){background:orange;}/*把第3、第6、第9、…、所有3的倍数的LI的背景设为橙色*/

第三种:倍数分组匹配

:nth-child(an+b) 与 :nth-child(an-b)

先对元素进行分组,每组有a个,b为组内成员的序号,其中字母n和加号+不可缺省,位置不可调换,这是该写法的标志,其中a,b均为正整数或0。如3n+1、5n+1。但加号可以变为负号,此时匹配组内的第a-b个。(其实an前面也可以是负号,但留给下一部分讲。)

例子:

li:nth-child(3n+1){background:orange;}/*匹配第1、第4、第7、…、每3个为一组的第1个LI*/

li:nth-child(3n+5){background:orange;}/*匹配第5、第8、第11、…、从第5个开始每3个为一组的第1个LI*/

li:nth-child(5n-1){background:orange;}/*匹配第5-1=4、第10-1=9、…、第5的倍数减1个LI*/

li:nth-child(3n±0){background:orange;}/*相当于(3n)*/

li:nth-child(±0n+3){background:orange;}/*相当于(3)*/

第四种:反向倍数分组匹配

:nth-child(-an+b)

此处一负一正,均不可缺省,否则无意义。这时与:nth-child(an+1)相似,都是匹配第1个,但不同的是它是倒着算的,从第b个开始往回算,所以它所匹配的最多也不会超过b个。

例子:

li:nth-child(-3n+8){background:orange;}/*匹配第8、第5和第2个LI*/

li:nth-child(-1n+8){background:orange;}/*或(-n+8),匹配前8个(包括第8个)LI,这个较为实用点,用来限定前面N个匹配常会用到*/

第五种:奇偶匹配

:nth-child(odd) 与 :nth-child(even)

分别匹配序号为奇数与偶数的元素。奇数(odd)与(2n+1)结果一样;偶数(even)与(2n+0)及(2n)结果一样。

附:例子效果图

选择器是jQuery最重要的部分之一,在jQuery中对事件处理,DOM操作和Ajax操作都依赖于选择器,熟练使用选择器,不仅能简化代码,还可以达到事半功倍的效果。

在说jQuery之前我们也可以回忆一下CSS的选择器,我想这对我们还算是比较熟悉,分别有:标签选择器、ID选择器、类选择器、群组选择器、后代选择器、通配选择符….等等选择器,具体语法说明在这里就不说了。而我们要说了jQuery中的选择器完全继承了CSS的风格,不过jQuery选择器适应通用性更好,CSS中很多选择器IE不支持,但jQuery支持常用的浏览器。

$的选择器部分,凡是运用符号:$,其返回值都是一个object。

下面分类别的简单介绍一下jQuery选择器:

1、基本选择器:通过标签元素id、class、标签名等来查找DOM元素

$(“#id名”),如$(“#test”)是选取了id为test的标签节点

$(“标签名”),如$(“p”)是选取了所有的P标签节点

$(“.class名”),如$(“.test”)是选取了所有class为test的标签节点

$(“*”),如$(“*”)是选取所有的标签元素

$(“标签名1,标签名2,…”),如$(“div,span,p.myClass”)是选取所有<div>,<span>和拥有class为myClass的<p>的一线标签元素。

2、层次选择器:通过DOM元素的层次关系来获取特定元素,包括后代元素、子元素、相依元素、兄弟元素等。

$(“标签名 标签名”),如$(“div span”)选取<div>里所有的<span>元素

$(“parent child”),如$(“div>span”)选取<div>元素下元素名是<span>的子元素

$(‘prev+next’)等价$(‘prev’).next(‘next’),如$(‘.one+div’)等价$(‘.one’).next(‘div’)是选取class为one的下一个<div>标签元素

$(‘prev~siblings’)等价$(‘prev’).nextAll(‘div’),如(‘#two~div’)等价$(‘#two’).nextAll(‘div’)是选取id为two的元素后面的所有<div>兄弟元素

3、过滤选择器:主要是通过一些过滤规则来筛选DOM元素,过滤规则与CSS中的伪类选择器语法相同,即选择器都以一个冒号(:)开头

$(“标签元素:first”),如$(“div:first”)是选取所有<div>元素中第一个<div>元素

原文:http://hi.baidu.com/meteoric_cry/blog/item/dcb48b19f75e6b4242a9ade0.html

Your email address will not be published. Required fields are marked *

*

使用新浪微博登陆

前端天空

致力于前端开发应用