jQuery中實現自訂方法的擴充

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   io   for   ar   

    JQuery封裝器提供了大量的方法,可以再頁面中直接使用。但是,沒有

任何一個庫可以滿足所有的需求,所以,JQuery庫提供了豐富的擴充功能

。以禁用一組表單元素為例,看看怎麼簡單有效在JQuery庫中添加自定

義的功能擴充。(JQuery沒有禁用表單元素的方法哦)

    上代碼:

 

   1: <!DOCTYPE>   2: <html lang="en">   3: <head>   4:     <title>Custom Method!</title>   5:     <meta http-equiv="content-type" content="text/html;charset=utf-8">   6:     <script type="text/javascript" src="js/jquery-2.1.1.js"></script>   7:     <script type="text/javascript">   8:         jQuery(function()   9:             {  10:                 $("form input").disable();  11:             });  12:     </script>  13: </head>  14: <body>  15:     <p>測試自訂方法禁用表單元素</p>  16:     <form>  17:         <input type="text" value="test"><br/>  18:         <input type="button" value="confirm" class="test">  19:     </form>  20: </body>  21: </html>

 

文字框和按鈕已經被禁用:

若只禁用按鈕,添加一個css類即可;

 

   1: jQuery(function()   2:             {   3:                 $("form input.test").disable();   4:             });

 

看看是不是禁用了按鈕:

 

實現也很簡單,開啟jquery-2.1.1.js源碼,沒有壓縮的,比較容易,我引入

的是jquery-2.1.1.js,然後添加下列代碼

 

   1: jQuery.fn.disable=function () {   2:         return this.each(function()   3:             {   4:                 if(this.disabled != null) this.disabled=true;   5:             });   6:     }

 

在按照上述案例的方式調用即可。

 

在《JQuery In Action》一書中,提到以$.fn.方法名方式擴充(P12),我

測試過,沒有實現禁用。錯誤之處,還望指正。

 

更多相關文章,去看看

 

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.