自訂JQuery外掛程式

來源:互聯網
上載者:User

標籤:許可權   學習   方法   ide   對象   str   func   頁面   需要   

           Jquery是個好東西,今天學習了怎麼自訂Jquery外掛程式,下面開始學習。1.使用外掛程式前的代碼
 
  1. //(1)根據按鈕添加分隔字元
  2. /** 查詢工具列中table中的所有td,去掉第一個td與最後一個td */
  3. var table = $("#tb > table");
  4. var tds = table.find("td:not(:first,:last)");
  5. /** 判斷td的個數 */
  6. if(tds.length > 0){
  7. /** 建立td添加到所有查詢到得td前面(相鄰元素) */
  8. $(‘<td><div class="dialog-tool-separator"></div></td>‘)
  9. .insertBefore(tds);
  10. }
  11. //(2)沒有一個許可權需要把工具列中table隱藏
  12. if(table.find("td").length == 1){
  13. table.hide();
  14. }
2.自訂外掛程式   (1)  自訂外掛程式的代碼要滿足下面的格式
 
  1. (function($){
  2. /**書寫自訂的JQuery方法的位置*/
  3. })(jQuery);
    (2)  自訂JQuery對象的方法按照下面格式自訂即可.
            注意: 在該方法裡面的this是指調用該方法的Jquery對象
 
  1. $.fn.自訂方法的名字 = function(){ 
  2. /** 邏輯代碼 */ 
  3. }
   (3)  完成的代碼        代碼:
 
  1. /**
  2. * jQuery工具外掛程式
  3. */
  4. (function($){
  5. /**
  6. * 最佳化表格上面的工具列外掛程式
  7. * (1)根據按鈕添加分隔字元
  8. * (2)沒有一個許可權需要把工具列中table隱藏
  9. */
  10. $.fn.formart4ToolBar = function(){
  11. /**
  12. * this:代表調用當前方法的JQuery對象 即$("#tb > table")
  13. */
  14. //(1)根據按鈕添加分隔字元
  15. /** 查詢工具列中table中的所有td,去掉第一個td與最後一個td */
  16. var tds = this.find("td:not(:first,:last)");
  17. /** 判斷td的個數 */
  18. if(tds.length > 0){
  19. /** 建立td添加到所有查詢到得td前面(相鄰元素) */
  20. $(‘<td><div class="dialog-tool-separator"></div></td>‘)
  21. .insertBefore(tds);
  22. }
  23. //(2)沒有一個許可權需要把工具列中table隱藏
  24. if(this.find("td").length == 1){
  25. this.hide();
  26. }
  27. };
  28. })(jQuery);
    在頁面中調用:  直接通過Jquery對象.方法名() 來調用    
 
  1. //調用自訂的外掛程式來最佳化頁面
  2. $("#tb > table").formart4ToolBar();


自訂JQuery外掛程式

相關文章

聯繫我們

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