jquery外掛程式編寫(以摺疊面板為例)

來源:互聯網
上載者:User

jquery外掛程式編寫(以摺疊面板為例)
??

建立一個 jQuery 外掛程式所推薦的方法是,先建立一個允許使用貨幣符號 ($) 的封裝器函數。在預設情況下,貨幣符號可能與其他 JavaScript 架構發生衝突,如果將外掛程式封裝在一個函數中,就不會出現衝突。所以推薦使用下面代碼建立jQuery外掛程式:

(function($) {    $.fn.accordion = function() {        // 添加外掛程式代碼    };})(jQuery);

 

上面代碼,jQuery 關鍵字被應用到封裝器函數中,這允許您在外掛程式中使用貨幣符號,就像使用 fn 屬性時那樣。封裝器函數就緒後,就可以在整個外掛程式的任何地方使用貨幣符號代替 jQuery 關鍵字,不會干擾其他第三方外掛程式。

 

外掛程式代碼

 

(function($) {  $.fn.accordion = function(options) {    return this.each(function() {      var dts = $(this).children('dt');      dts.click(onClick);      dts.each(reset);    });     function onClick() {      $(this).siblings('dt').each(hide);      if($(this).next().is(:hidden)){      $(this).next().slideDown('fast');        return false;      }      if($(this).next().is(:visible)){         $(this).next().slideUp('fast');        return false;      }          }     function hide() {      $(this).next().slideUp('fast');    }     function reset() {      $(this).next().hide();    }  }})(jQuery);

 

頁面代碼

<script src=http://libs.baidu.com/jquery/1.9.0/jquery.js></script><script src=aa.js></script>//外掛程式js <script>$(function(){//調用你的自訂外掛程式 $('dl#my-accordion').accordion();//調用外掛程式});</script>
Section 1
Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.
Section 2
Vestibulum a velit eu ante scelerisque vulputate.
Section 3
Nam mi. Proin viverra leo ut odio. Curabitur malesuada.
Section 4
Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc.

效果如下

 

 

這就是我們想要的效果

 

聯繫我們

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