jQuery2.0+中自訂選取器外掛程式

來源:互聯網
上載者:User

標籤:

jQuery中自訂外掛程式,主要可以分為三大類:封裝對象方法的外掛程式、封裝全域函數的外掛程式、選取器外掛程式。
以下分享低版本jQuery和jQuery2.0+版本中選取器外掛程式的使用:

原理:
       jQuery的選擇符解析器首先會使用一組Regex來解析選取器,然後針對解析出的每個選擇符執行一個函數,稱為選取器函數。最後根據這個選取器函數的傳回值為true還是false來決定是否保留這個元素。這樣就可以找到匹配的元素節點

先上代碼,在解析:

 低版本jQuery

<html xmlns="http://www.w3.org/1999/xhtml"><head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <!--   引入jQuery -->    <script src="../../scripts/jquery-1.4.js" type="text/javascript"></script>    <script    type="text/javascript">        //外掛程式編寫        ;(function($) {            $.extend(jQuery.expr[":"], {                between :    function( a , i ,m ) {                    var tmp=m[3].split(","); //將傳遞進來的m[3]以逗號為分隔字元,切成一個數組                    return tmp[0]-0<i&&i<tmp[1]-0;                }            });        })(jQuery);        //外掛程式應用        $(function(){            $("div:between(2,5)").css("background","white");        })    </script></head><body><div style="background:red">0</div><div style="background:blue">1</div><div style="background:green">2</div><div style="background:yellow">3</div><div style="background:gray">4</div><div style="background:orange">5</div></body></html>
View Code

注意:這裡參數i是有效

jQuery2.0+版本

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <script src="../Content/js/jquery-2.1.3.js"></script>    <script type="text/javascript">        ; (function ($) {            $.extend(jQuery.expr[":"], {                between: function (a, i, m) {                                       var tmp = m[3].split(","); //將傳遞進來的m[3]以逗號為分隔字元,切成一個數組                    return tmp[0]< $(a).index() && $(a).index() < tmp[1];                }            });        })(jQuery);    </script>    <script type="text/javascript">        $(function () {            $("div:between(2,5)").css("background", "white");        });    </script></head><body>    <div style="background-color: red">0</div>    <div style="background-color: yellow">1</div>    <div style="background-color: green">2</div>    <div style="background-color: blue">3</div>    <div style="background-color: olive">4</div>    <div style="background-color: orange">5</div></body></html>
View Code

注意在高版本中,無法使用參數i,通過index()替換即可!
---------------------------------------------------------分割線-------------------------

其他說明:
例子中用到了function(a,i,m){//……},如下說明:

參數 說明
a 指向的事當前遍曆到的DOM元素
i 指的事當前遍曆到的DOM元素的索引值,從0開始。
(注意:高版本中不起作用,可以用$(a).index()來代替)
m 其有jQuery正則解析引擎進一步解析後的產物,是一個長度為4的數組:
m[0] 為jQuery選取器進行匹配內容。
例如:$(“div:gt(1)”),m[0]為gt(1)
m[1] 選取器的引導符,匹配例子中的”:”.
當然並非只能使用”:”後面跟上選取器,使用者可以自訂其他的選取器引導符。
m[2] 確定調用哪個選取器函數,例如上例中gt
m[3] 選取器函數傳入的參數,例如上例中1
m[3]為編寫選取器最重要一個參數
m[4] 較少使用,指代選取器函數中嵌套函數的參數.
例如:div:l(ss(dd)),此時就為(dd)

例子引用資料&&更多學習可以參考:《鋒利的jQuery》

jQuery2.0+中自訂選取器外掛程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.