版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、總結(jié)jquery插件開發(fā)中的一些要點這篇文章主要介紹了總結(jié)jquery插件開發(fā)中的一些要 點,包括命名空間與事件events等重點和難點部分的講解, 要的朋友可以參考下基礎(chǔ)1、jquery插件開發(fā)主要使用如下兩個方法:1. 1、添加靜態(tài)方法jquery. extend (object);為擴展jquery本身,為類添加新的方法,可以理解文 添加靜態(tài)方法。$ extend (nbsp;addmethod :function(a ,b) returna +b;nbsp;/$. addmethod (1,2);/ret urn3);1.2、添加成員方法jqu ery. fn. exte nd (ob
2、ject);jquery. fn 二jquery. pro totype給jq uery對象添加方法,對jquery. pro totype進(jìn)行 擴展,為jquery類添加成員方法:$ fn. ex tend(nbsp;nbsp; getl nput text: fu net ion () nb sp;nbsp;n bsp; $ (this) click(func tion() nbsp ;nbsp; nbs p; nbsp; aler t ($ (this) v al () : nbsp; nbspjnbsp;);nbsp;nbsp ;nbsp;);$ (#userna me) getlnp
3、 uttext ();2、一個通用的框架: 以下是一個通用的框架: (fu notion($) n bsp;nbsp;$. fn. yourpl uginname=fu nction(options) nbsp;nbsp;nbsp;/各種屬性和參數(shù)nb sp;nbsp;n bsp;varopti ons=$ exten d(defaults, options) ;nb sp;nbsp; n bsp; this ea ch (function () nbsp;n bsp;nbsp;nb sp;/插件的實現(xiàn)代碼 nbsp;nbsp;);nbsp;nbs p; ;nbsp;) (jquery);關(guān)于
4、$ extend (defaults, op tions);就是通過合并default s和options來擴展defaults,實現(xiàn)插件默認(rèn)參數(shù)的功能。實踐1、聲明插件名稱:添加一個函數(shù)到 jq uery. fn (jqu ery. prototy pe)對 象,該函數(shù)的名稱就是你的插件名稱:jquery. fn. myplugin=fu notion() nbsp: /doyo urawesomepl uginstuffhe re;在命名不與jquery其他函數(shù)沖突的情況,可以使用閉 包的方式使用$符號:(f unction ($) nbsp; $. fn . myplugin=f unc
5、tion() nbsp;/doy ourawesomep luginstuffh erenbsp;)(jquery);2、插件中的上下文:jquery接收一個回調(diào),該回調(diào)本身就指向了dom,直接 使用this就相當(dāng)于我們平時的$(this)的情況:(function($) nbsp; $ f n. myplugin=function () nbsp;/th ere' snoneed todo$ (this)becausenb sp;/his/zisalreadyaj queryobjectnbsp;/$ (this)wouldb ethesameas$ ($ (j ttelemen t
6、,);nbsp ; this fadel n c normat , function () nbspjnbsp ;/thethisk eywordisado melement nbs p;);nbsp;;)(jquery);$ (j #elem ent'). myplu gin ();下面是一個簡單的jquery插件,實現(xiàn)獲取所有di v的 最大高度:(fun ction ($) nbsp;$ fn. m axheight=fu notion() nb sp;varmax=0 ;nbsp;thi s. each(func tion() nb sp;nbsp;max =math. max
7、(m ax, $ (this) height (); n bsp;);nbsp ;returnmax; nbsp;) (j query);vartallest=$( 'div,) maxh eight();/returnsthehe ightoftheta llestdiv3、維護(hù)鏈接性:前面的示例返回一個整數(shù)值最高的div,但是通常的意 圖僅在某種程度上是僅修改插件的元素集合,并將它們傳遞 到下一個鏈中的方法。這樣的jquery的設(shè)計優(yōu)雅的地方。 所以保持鏈接性放到一個插件,您必須確保您的插件返回這 個關(guān)鍵字。(fun ction ($) nbsp;$ fn. 1 ockdimens
8、io ns=function (type) nbsp ; nbsp;ret urnthis eac h(function() nbspjnb sp; var$this =$ (this);nbsp;nbsp; i f (! type | | ty pe二二'width') nbsp;nb sp; $this wi dth ($this w idth ();nbs p;nbsp;n bsp;nbsp:if (! type| | typ e二heigh) nbspjnb sp; $this he ight ($this height ();n bsp;nbsp;n bsp;);nb
9、sp ;)(jquer y);$ (' div '). lockdime nsions (' wid th') css (' c olor',' red,);因為插件返回t his關(guān)鍵字的范圍,它維護(hù)鏈接性, jquer y可以繼續(xù)利用jque ry方法,女口. css。所以,如果 你的插件不返回一個內(nèi)在價值,你應(yīng)該總是返回thiso4、參數(shù)的傳遞,def aults 和 0ptio ns:(functio n($) nbsp ; $ fn. toolt ip=function (options) n bsp;nbsp; /creates
10、omedefauits, e xtendingthemwithanyopt ionsthatwer eprovidednbsp;varset tings=$ ext end(nbsp ; nbsp;' loca tion,nbsp; n bsp; :' top',nbsp; nbsp ;' backgroun d-color' :' b lue'nbsp;, options);nbsp;return this each(f unction() n bsp;nbsp;nbsp;nbsp;/tooltipplu gincodeherenbsp
11、;);nbs p;) (jque ry);$ (' div'). tooltip (nbsp;' loc at ion' :,lef t,);通過$. extend合并默認(rèn)參數(shù)和調(diào)用者傳入的參數(shù)。5、命名空間:正確的命名空間在插件開發(fā)中是一個非常重要的部 分。正確的命名空間,可以確保你的插件將有一個很低的 幾率在同一個頁面上被他插件或代碼覆蓋。在任何情況下都不應(yīng)該在一個插件的jqueryfn對象 中聲稱多個名稱空間。(functi on ($) nbs p;$. fn. tool tip=functio n(options) nbsp; nbsp;/this n
12、bsp;nbsp; $ f ri. tooltipsh ow=function () nbsp;/is nbsp;n bsp; $. fn. to oltiphide=f unction () n bsp; nbsp;/bad nbsp;nbsp;$ fn. tooltipupda te=function (content) n bsp; nbsp;/!nbsp;nbs p;) (jque ry);你應(yīng)該收集所有的方法到一個對象化字面,并且通過 方法的字面值進(jìn)行調(diào)用:(fun ction($) nb sp; varmetho ds=nbsp; init :functi on (options)
13、 nbsp;nbs p;nbsp;/th isnbsp;nbsp; s how: functio n () nbspjnb sp;/isnbsp ;,nbsp;h ide: functio n () nbsp;nb sp;nbsp;/g 00dnbsp;,nbspjupdat e: function (content) nb sp;nbsp;nbs p;/! ! !nbsp ;nbsp;nbsp ;nbsp; $ fn. tooltip =function (m ethod) nb sp;/method callinglogi cnbsp;if(methodsmet hod) nbs p;nb
14、sp;retu rnmethodsm ethod. apply (this, arra y. prototype slice. call (arguments, 1); nbsp: elseif(typ eofmethod二二二'object | | !method) nbsp;nbsp;r eturnmethod s. init .appl y(this, argu merits);nbsp ;elsenb sp;nbsp;$ e rror c metho d' +method+, doesnotexis tonjquery. t ooltip');nb sp;nbs
15、p;nb sp;) (jqu ery);/call stheinitmet hod$(' div '). tooltip () ;nbsp;/ca llstheinitm ethod$ (' div '). tooltip (nbsp;foo:' bar');/ca llsthehidem ethod$ (? d iv'). toolti p(' hide') : n bsp;/cal lstheupdate method$ (' div'). tooltip c update,' thisisth
16、enew to oltipc ontent!?;這種類型的方法封裝和體系結(jié)構(gòu)在jquery插件社區(qū)中 是一個標(biāo)準(zhǔn),它適用于無數(shù)的插件,包括jqueryui插件和 小部件。6、even ts:bind方法允許通過命名空間的方式綁定事件,如果你 的插件綁定了事件,可以通過命名空間的方式,在解除綁 定事件時,你也可以這樣做,來防止影響到其他的事件。 可以通過".it :namespaceg t;”的方式來設(shè)置綁定事件的 命名空間。(fu nction ($) n bsp; varmeth ods= nbsp ;nbsp; init:function (op tions) nb sp;nbs
17、p;ret urnthis eac h(function() nbspjnb sp;nbsp;$(w indow). bind c resize to oltip' , meth ods reposit ion);nbsp;n bsp;);nbsp ;nbsp;,n bsp; nbsp; de stroy: funct ion () nbs p;nbsp;retu rnthis each (function() nbsp;nbs p;nbsp; $ (wi ndow). unbin d( tooltip '); nbsp;nbs p;) nbspjnb sp;,nbsp ;nb
18、sp;repos ition:funct ion() nbsp;nbsp;nbsp;/ nbsp;nb sp;nbsp;,nbsp;nbsp; show: functi on() nbsp;n bspjnbsp;/ nbsp;nbs p;nbsp;,nbsp;nbsp;h ide:functio n() nbsp;nb sp; / nbs p:nbsp;nbsp ;,nbsp;n bsp;update:fimction(co ntent) nbsp ; nbspjnbsp;/ nbsp; n bsp: nbsp;;nbsp; $ f n. tooltip=f unction (met hod)
19、 nbsp :if (methods method)nbsp;nbsp; returnmetho dsmethod apply(this,array. proto type. slice. call (argume nts, 1);n bsp;elseif(typeofmethod=,object'| |!method ) nbspjnb sp:returnme thods init. apply(this, arguments);nbsp;else nbsp;nbsp ;$error c m ethod? +meth od+? doesnot existonjque ry. tool
20、tip');nbsp;nbs p; nbsp;;)(jquery);$(' #ftm'). too ltip();/so metimelater $ (' #fu n,). tooltip c des troy');7、data:關(guān)于data方法可以參考官方的文檔:http: /docs, jque ry. com/data ,既是在頁面的元素中綁定存儲 數(shù)據(jù)。這里通過編寫插件,實現(xiàn)在頁面中的每個元素都綁定 一些當(dāng)前的狀態(tài)或者內(nèi)容。(function($) nbsp; varme thods=nb spjnbsp; ini t :f unction (o
21、ptions) nbsp;nbsp;r eturnthis e ach (functio n () nbsp;nbsp;nbsp;v ar$this=$ (t his),nbsp :nbsp:nbsp:nbsp:data=$this data(' tooltip'), nbsp; nbsp; nbsp; nbsp; t ooltip=$ c 1 t; div/gt;? , nbspjnbs p;nbsp;nbsp : text: $this attr (j titl e ) nbsp;n bsp;nbsp;nb sp;);nbsp;nbsp;nbsp;/ifthepluginh
22、asn tbeeninitializ edyetnbsp ;nbsp;nbsp; if (!data) n bsp;nbsp;nb sp;/*nbsp ; domoresetu pstuffheren bsp;*/nbs p;nbsp;nbsp ;$(this)datactooltip ' , nbsp;n bsp;nbsp;nb sp; target: $this,nbsp :nbsp;nbsp;nbsp;toolti p:tooltipnb sp;nbsp;nbs p;); nbsp;n bspjnbsp; n bspjnbsp;);nbsp;nbsp;,nbsp;nb sp: destroy: function() nbsp;nbsp jreturnthis each(funct ion() nbs p;nbsp;nbsp ; var$this=$ (this),nb sp;nbsp;nbs p;nbsp;data =$this .data ('toolti
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年湘教新版九年級生物上冊月考試卷含答案
- 2025年北師大新版九年級地理下冊月考試卷含答案
- 2025年華東師大版九年級生物上冊階段測試試卷含答案
- 2025年冀教版九年級歷史下冊階段測試試卷含答案
- 2025年冀教版選擇性必修1歷史下冊階段測試試卷
- 2025年上教版七年級生物下冊階段測試試卷
- 2025年外研版九年級歷史上冊月考試卷
- 二零二五版離婚協(xié)議書起草與子女撫養(yǎng)權(quán)維護(hù)服務(wù)合同4篇
- 二零二五版借貸房屋買賣合同糾紛調(diào)解服務(wù)合同4篇
- 二零二五版木結(jié)構(gòu)建筑能耗數(shù)據(jù)采集與分析合同4篇
- 電力系統(tǒng)動態(tài)仿真與建模
- 蝦皮shopee新手賣家考試題庫及答案
- 四川省宜賓市2023-2024學(xué)年八年級上學(xué)期期末義務(wù)教育階段教學(xué)質(zhì)量監(jiān)測英語試題
- 價值醫(yī)療的概念 實踐及其實現(xiàn)路徑
- 2024年中國華能集團(tuán)燃料有限公司招聘筆試參考題庫含答案解析
- 《紅樓夢》中的男性形象解讀
- 安全生產(chǎn)技術(shù)規(guī)范 第49部分:加油站 DB50-T 867.49-2023
- 《三國演義》中的語言藝術(shù):詩詞歌賦的應(yīng)用
- 腸外營養(yǎng)液的合理配制
- 消防安全教育培訓(xùn)記錄表
- 2023年河南省新鄉(xiāng)市鳳泉區(qū)事業(yè)單位招聘53人高頻考點題庫(共500題含答案解析)模擬練習(xí)試卷
評論
0/150
提交評論