jQuery 自訂選取器

來源:互聯網
上載者:User

jQuery 自訂選取器
/* Custom selectors for your convenience.  譯: 提供自訂選擇方便你使用。 *//* Use as $("img:below-the-fold").something() or 譯: 你可以這樣使用 $("img:below-the-fold").something() 或 *//* $("img").filter(":below-the-fold").something() which is faster 譯: $("img").filter(":below-the-fold").something() 這樣更快 */// 簡單翻譯了下不知道準不準確。。$.extend($.expr[":"], {    "below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },    "above-the-top"  : function(a) { return !$.belowthefold(a, {threshold : 0}); },    "right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },    "left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },    "in-viewport"    : function(a) { return $.inviewport(a, {threshold : 0}); },    /* Maintain BC for couple of versions. */    "above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },    "right-of-fold"  : function(a) { return $.rightoffold(a, {threshold : 0}); },    "left-of-fold"   : function(a) { return !$.rightoffold(a, {threshold : 0}); }});好神奇的說,有木有。。雖然之前也有簡單的看過 jQuery 源碼,可是 Sizzle 選取器引擎部分我直接跳過了,所以完全沒注意到這塊功能。出於好奇心,當然要去學習下怎麼自訂選取器。 翻了下百度Google,只找到一篇靠譜的中文部落格。。看樣子這東西確實冷門啊。。仔細想想確實沒多大必要自訂。。不過以學習為目標,多學點總是好的。大家可以去看下這篇文章《jQuery Custom Selector JQuery自訂選取器》 基本格式: 文本$.expr[':'].test = function(obj, index, meta, stack) {    /* obj   - is a current DOM element 當前DOM元素       index - the current loop index in stack 當前元素在stack中的索引,       meta  - meta data about your selector !!! 用來存參數值,詳見帶參數的自訂選取器。       stack - stack of all elements to loop 選取器所選中的元素集。          Return true to include current element 返回 true 就包含當前元素       Return false to explude current element 返回 false 就拋棄當前元素    */};調用方法: 文本$('.someClasses:test').doSomething();帶參數調用: 文本$('.someClasses:test(argument)').doSomething();$('.someClasses:test("arg1, arg2")').doSomething();$(".someClasses:test('arg1, arg2')").doSomething();還記得剛才 meta 參數嗎?例如這樣調用: 文本$('.someClasses:test(argument)').doSomething();meta 就會得到如下格式的數組: 文本[    ':test(argument)', // full selector 整個自訂選取器    'test',            // only selector 自訂選取器名稱    '',                // quotes used   使用了什麼引號(很明顯這裡沒有)    'argument'         // parameters    參數]例如調用: 文本$('.someClasses:test("arg1, arg2")').doSomething();meta 就會得到如下格式的數組: 文本[    ':test("arg1, arg2")', // full selector 整個自訂選取器    'test',                // only selector 自訂選取器名稱    '"',                   // quotes used   使用了什麼引號(這裡用的是雙引號)    'arg1, arg2'           // parameters    參數]那我們來寫個簡單的選取器,當作練習吧。就寫個剛才那個選取器把,帶參數好了。 文本$("a:test(haha)");自訂選取器代碼為: 文本運行$.expr[':'].test = function(obj, index, meta, stack) {    var txt = obj.innerHTML,        key = meta[3];    return txt.indexOf(key) > -1;};// 測試$('#navList a:test(JavaScript)').each(function () {    console.log(this.innerHTML);});點擊右側運行看看。。 沒啥技術含量,不過確實是有意思的技巧。  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.