当前位置:编程学习 > JAVA >>

jQuery:掌握选择器

选择器至少可以追溯到“CSS选择器”。jQuery的源代码中内嵌了一个叫Sizzle的对象,其实就是选择器了。在jQuery官网上显示Sizzle属于“Other jQuery Foundation Projects”,Sizzle能够独立为一个单独的项目,由此不难体会到选择器的重要性。看看下面三个页面,相比之下,jQuery选择器官方文档看起来是最“乱”的。
http://www.w3.org/TR/css3-selectors
CSS选择器W3C标准文档
 
https://github.com/jquery/sizzle/wiki/Sizzle-Documentation
Sizzle文档
 
http://api.jquery.com/category/selectors/
jQuery选择器官方文档
jquery1.9.0源代码有这样一行:
jQuery.find = Sizzle; 
 
导入jquery.js和sizzle.js,可以看到jQuery.find和Sizzle确实是一回事。
 
iJs.showObjectNames("window.jQuery.find");  
iJs.showObjectNames("window.Sizzle"); 
 
    [Object] window.jQuery.find
        |--[function] attr
        |--[function] compile
        |--[function] contains
        |--[function] error
        |--[function] getText
        |--[function] isXML
        |--[function] matches
        |--[function] matchesSelector
        |--[function] setDocument
        |--[function] uniqueSort
        |--[object] selectors
    
    [Object] window.Sizzle
        |--[function] attr
        |--[function] compile
        |--[function] contains
        |--[function] error
        |--[function] getText
        |--[function] isXML
        |--[function] matches
        |--[function] matchesSelector
        |--[function] setDocument
        |--[function] uniqueSort
        |--[object] selectors
 
既然Sizzle自称"supports virtually all CSS 3 Selectors",那么不妨就参考下面W3C描述吧,再难找到更好的文档片断了(点击链接可查看语法细节),不是么?
 
模式 含义 链接 版本
* any element Universal selector 2
E an element of type E Type selector 1
E[foo] an E element with a "foo" attribute Attribute selectors 2
E[foo="bar"] an E element whose "foo" attribute value is exactly equal to "bar" Attribute selectors 2
E[foo~="bar"] an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar" Attribute selectors 2
E[foo^="bar"] an E element whose "foo" attribute value begins exactly with the string "bar" Attribute selectors 3
E[foo$="bar"] an E element whose "foo" attribute value ends exactly with the string "bar" Attribute selectors 3
E[foo*="bar"] an E element whose "foo" attribute value contains the substring "bar" Attribute selectors 3
E[foo|="en"] an E element whose "foo" 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 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:visited
E:active an E element during certain user actions The user action pseudo-classes 1 and 2
E:hover
E:focus
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 a user inte易做图ce element E which is enabled or disabled The UI element states pseudo-classes 3
E:disabled
E:checked a user inte易做图ce 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::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 易做图 selector s Negation pseudo-class 3
E F an F element descendant of an E element Descendant combinator 1
E > F an F element child of an E element Child combinator 2
E + F an F element immediately preceded by an E element Adjacent sibling combinator 2
E ~ F an F element preceded by an E element General sibling combinator 3
需注意,jQuery()和jQuery.find()返回的对象类型是不一样的,前者是jQuery.fn,后者是Sizzle。例如,jQuery('html body div#dbg');和jQuery.find('html body div#dbg');都是“选择”了id为dbg的div,但是前者表示为jQuery.fn对象,后者表示为Sizzle对象。
选择器的语法是有标准的,逐一尝试每一种写法没有必要,今天学会了也许明天就忘记,不如有文档在手,用到时再翻阅。需要想一想的是选择器的价值所在,从根本上来讲,选择器就是帮助我们避免了遍历DOM(或者也包括XML?)的麻烦。一个两个的遍历代码其实也不难写,但是网页上的互动多了就麻烦了,下面是一个隔行选取的代码片断,不难体会其中运用选择器的奥妙所在:
<div> www.zzzyk.com
    <b>...测试1...</b> 
    <b>...测试2...</b> 
    <b>...测试3...</b> 
    <b>...测试4...</b> 
</div> 
 
<script language="javascript">   
 
补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,