選擇器至少可以追溯到“CSS選擇器”。jQuery的源代碼中內(nèi)嵌了一個(gè)叫Sizzle的對象,其實(shí)就是選擇器了。在jQuery官網(wǎng)上顯示Sizzle屬于“Other jQuery Foundation Projects”,Sizzle能夠獨(dú)立為一個(gè)單獨(dú)的項(xiàng)目,由此不難體會(huì)到選擇器的重要性。看看下面三個(gè)頁面,相比之下,jQuery選擇器官方文檔看起來是最“亂”的。
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)西烏珠穆沁免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
- http://www.w3.org/TR/css3-selectors
- CSS選擇器W3C標(biāo)準(zhǔn)文檔
- https://github.com/jquery/sizzle/wiki/Sizzle-Documentation
- Sizzle文檔
- http://api.jquery.com/category/selectors/
- jQuery選擇器官方文檔
jquery1.9.0源代碼有這樣一行:
- jQuery.find = Sizzle;
- iJs.showObjectNames("window.jQuery.find");
- iJs.showObjectNames("window.Sizzle");
語法 | 含義 | 鏈接 | 版本 |
---|---|---|---|
* | any element | 更多 | 2 |
E | an 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:root | an 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-child | an E element, first child of its parent | 更多 | 2 |
E:last-child | an E element, last child of its parent | 更多 | 3 |
E:first-of-type | an E element, first sibling of its type | 更多 | 3 |
E:last-of-type | an E element, last sibling of its type | 更多 | 3 |
E:only-child | an E element, only child of its parent | 更多 | 3 |
E:only-of-type | an E element, only sibling of its type | 更多 | 3 |
E:empty | an 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:target | an 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:checked | a user interface element E which is checked (for instance a radio-button or checkbox) | 更多 | 3 |
E::first-line | the first formatted line of an E element | 更多 | 1 |
E::first-letter | the first formatted letter of an E element | 更多 | 1 |
E::before | generated content before an E element | 更多 | 2 |
E::after | generated content after an E element | 更多 | 2 |
E.warning | an E element whose class is "warning" (the document language specifies how class is determined). | 更多 | 1 |
E#myid | an E element with ID equal to "myid". | 更多 | 1 |
E:not(s) | an E element that does not match simple selector s | 更多 | 3 |
E F | an F element descendant of an E element | 更多 | 1 |
E > F | an F element child of an E element | 更多 | 2 |
E + F | an F element immediately preceded by an E element | 更多 | 2 |
E ~ F | an F element preceded by an E element | 更多 | 3 |
選擇器的語法是有標(biāo)準(zhǔn)的,逐一嘗試每一種寫法沒有必要,今天學(xué)會(huì)了也許明天就忘記,不如有文檔在手,用到時(shí)再翻閱。需要想一想的是選擇器的價(jià)值所在,從根本上來講,選擇器就是幫助我們避免了遍歷DOM(或者也包括XML?)的麻煩。一個(gè)兩個(gè)的遍歷代碼其實(shí)也不難寫,但是網(wǎng)頁上的互動(dòng)多了就麻煩了,下面是一個(gè)隔行選取的代碼片斷,不難體會(huì)其中運(yùn)用選擇器的奧妙所在:
- ...測試1...
- ...測試2...
- ...測試3...
- ...測試4...