jQuery:掌握选择器

选择器至少可以追溯到“CSS选择器”。jQuery的源代码中内嵌了一个叫Sizzle的对象,其实就是选择器了。在jQuery官网上显示Sizzle属于“Other jQuery Foundation Projects”,Sizzle能够独立为一个单独的项目,由此不难体会到选择器的重要性。看看下面三个页面,相比之下,jQuery选择器官方文档看起来是最“乱”的。

 
 
  1. http://www.w3.org/TR/css3-selectors
  2. CSS选择器W3C标准文档
  3.  
  4. https://github.com/jquery/sizzle/wiki/Sizzle-Documentation
  5. Sizzle文档
  6.  
  7. http://api.jquery.com/category/selectors/
  8. jQuery选择器官方文档

jquery1.9.0源代码有这样一行:

 
 
  1. jQuery.find = Sizzle; 
 
导入jquery.js和sizzle.js,可以看到jQuery.find和Sizzle确实是一回事。
 
 
 
  1. iJs.showObjectNames("window.jQuery.find");  
  2. 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描述吧,再难找到更好的文档片断了(点击链接可查看语法细节),不是么?
 (备注:这个Table贴过来总是出现显示问题,于是对html代码进行了编辑,其中一个替换是(<a\shref="[^">]*">)[^<]*(</a>)替换为$1更多$2)
语法含义链接版本
*any element更多2
Ean element of type E更多1
E[foo]an E element with a "foo" attribute更多2
E[foo="bar"]an E element whose "foo" attribute value is exactly equal to "bar"更多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"更多2
E[foo^="bar"]an E element whose "foo" attribute value begins exactly with the string "bar"更多3
E[foo$="bar"]an E element whose "foo" attribute value ends exactly with the string "bar"更多3
E[foo*="bar"]an E element whose "foo" attribute value contains the substring "bar"更多3
E[foo|="en"]an E element whose "foo" attribute has a hyphen-separated list of values beginning (from the left) with "en"更多2
E:rootan E element, root of the document更多3
E:nth-child(n)an E element, the n-th child of its parent更多3
E:nth-last-child(n)an E element, the n-th child of its parent, counting from the last one更多3
E:nth-of-type(n)an E element, the n-th sibling of its type更多3
E:nth-last-of-type(n)an E element, the n-th sibling of its type, counting from the last one更多3
E:first-childan E element, first child of its parent更多2
E:last-childan E element, last child of its parent更多3
E:first-of-typean E element, first sibling of its type更多3
E:last-of-typean E element, last sibling of its type更多3
E:only-childan E element, only child of its parent更多3
E:only-of-typean E element, only sibling of its type更多3
E:emptyan E element that has no children (including text nodes)更多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)更多1
E:active
E:hover
E:focus
an E element during certain user actions更多1 and 2
E:targetan E element being the target of the referring URI更多3
E:lang(fr)an element of type E in language "fr" (the document language specifies how language is determined)更多2
E:enabled
E:disabled
a user interface element E which is enabled or disabled更多3
E:checkeda user interface element E which is checked (for instance a radio-button or checkbox)更多3
E::first-linethe first formatted line of an E element更多1
E::first-letterthe first formatted letter of an E element更多1
E::beforegenerated content before an E element更多2
E::aftergenerated content after an E element更多2
E.warningan E element whose class is "warning" (the document language specifies how class is determined).更多1
E#myidan E element with ID equal to "myid".更多1
E:not(s)an E element that does not match simple selector s更多3
E Fan F element descendant of an E element更多1
E > Fan F element child of an E element更多2
E + Fan F element immediately preceded by an E element更多2
E ~ Fan F element preceded by an E element更多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?)的麻烦。一个两个的遍历代码其实也不难写,但是网页上的互动多了就麻烦了,下面是一个隔行选取的代码片断,不难体会其中运用选择器的奥妙所在:

 
 
  1. <div> 
  2.     <b>...测试1...</b> 
  3.     <b>...测试2...</b> 
  4.     <b>...测试3...</b> 
  5.     <b>...测试4...</b> 
  6. </div> 
  7.  
  8. <script language="javascript">   
  9.     var $myObj = jQuery('div b:nth-child(even)');//选择器  
  10.     $myObj.each(  
  11.         function(i){  
  12.             var tTemp = jQuery(this).text();  
  13.             iJs.put(tTemp);//输出选择结果  
  14.         });  
  15. </script> 
调试信息:
     ...测试2...
     ...测试4...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值