jQuery外掛製作簡單介紹
將程式碼以外掛形式呈現將會是一種非常好的選擇,因為這樣程式碼的可重用性將會大大加強,下面就來簡單介紹一下如何製作jQuery外掛,希望能夠給需要的朋友帶來一定的幫助。
一.jQuery物件(類)的靜態函式:
程式碼例項如下:
[JavaScript] 純文字檢視 複製程式碼jQuery.foo = function() { alert("螞蟻部落歡迎您"); };
完整程式碼:
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>自定義外掛-螞蟻部落</title> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> jQuery.foo=function(){ alert("螞蟻部落歡迎您"); }; jQuery.foo(); </script> </head> <body> </body> </html>
如果靜態函式較多的話可能會使名稱容易混淆,那麼採用名稱空間模式將是一個良好的選擇。
程式碼如下:
[JavaScript] 純文字檢視 複製程式碼jQuery.foos={ fun1:function(){ console.log("螞蟻部落一"); }, fun2:function(){ console.log('螞蟻部落二'); } }
二.物件例項方法:
程式碼如下:
[JavaScript] 純文字檢視 複製程式碼(function($){ $.fn.setbgColor=function(){ $("#thediv").css("backgroundColor","blue"); }; })(jQuery);
完整程式碼如下:
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>自定義外掛-螞蟻部落</title> <style type="text/css"> #thediv { width:100px; height:100px; background-color:red; } </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> (function($){ $.fn.setbgColor=function(){ $("#thediv").css("backgroundColor","blue"); }; })(jQuery); $(document).ready(function(){ $('#thediv').setbgColor(); }) </script> </head> <body> <div id="thediv"></div> </body> </html>
相關文章
- 製作一個超簡單的全屏外掛(基於JQuery)jQuery
- jQuery Validate簡單介紹jQuery
- cookie外掛-jquery.cookie.js介紹CookiejQueryJS
- Jquery外掛Nicescroll 製作漂亮的滾動條jQuery
- chatgpt~外掛介紹ChatGPT
- 一個簡單的以視訊作為網頁背景的jQuery 外掛網頁jQuery
- SVG繪製直線簡單介紹SVG
- logstash常用外掛介紹
- rematch常用外掛介紹REM
- 如何製作 Sketch 外掛
- 3.03 模組外掛介紹
- MySQL審計外掛介紹MySql
- VS2010常用外掛介紹之Javascript外掛JavaScript
- jQuery和css3側邊欄滑出式圖片介紹外掛jQueryCSSS3
- jQuery的事件機制,事件物件介紹,外掛機制,多庫共存,each()jQuery事件物件
- apisix~authz-keycloak外掛介紹API
- 【轉】jenkins外掛pipeline使用介紹Jenkins
- MySQL連線控制外掛介紹MySql
- RPC簡單介紹RPC
- Python簡單介紹Python
- KVM簡單介紹
- RMI簡單介紹
- HTML簡單介紹HTML
- HTML 簡單介紹HTML
- JavaScript 簡單介紹JavaScript
- CSS 簡單介紹CSS
- ajax簡單介紹
- SVG簡單介紹SVG
- Clickjacking簡單介紹
- 【Pandas】簡單介紹
- Map簡單介紹
- JSON簡單介紹JSON
- ActiveMQ簡單介紹MQ
- jQuery簡單實用的響應式固定側邊欄外掛jQuery
- JQuery模板外掛-jquery.tmpljQuery
- VS Code外掛開發介紹(二)
- PostCSS 常用外掛與語法介紹CSS
- Three系列1_CinematicCamera外掛介紹
- Git_簡單介紹Git