日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实例说明扩展JQuery方式

發(fā)布時間:2025/7/14 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实例说明扩展JQuery方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如何擴(kuò)展Jquery?

1. 有兩種方式來擴(kuò)展JQuery,分別是:

$.extend(object): 為域名jQuery增加方法,為靜態(tài)方法,全局方法,任何人都可以調(diào)用,調(diào)用的時候,需要直接加上$或者jQuery前綴。

$.fn.extend(object): $.fn = $.prototype, 因此可以從這里看出,這張擴(kuò)展方式是在為每個jQuery對象增加方法,因?yàn)槊總€jQuery對象的實(shí)例,都可以獲得在這里擴(kuò)展的方案。調(diào)用的時候,不需要添加特殊的前綴,直接選取jQuery對象之后,就可以調(diào)用了。

2. jQuery是一個封裝的非常好的類,如果使用$(“#id”)可以生成一個jQuery對象,但是注意,jQuery對象并不是真正的HTML標(biāo)記所對應(yīng)的對象,而是包含一個標(biāo)記所對應(yīng)的數(shù)組,數(shù)組中的成員才是標(biāo)記所對應(yīng)的對象。

?

下面代碼可以演示這兩種方式:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"/><title></title><script type="text/javascript" src="../lib/jquery-1.6.3.js"></script><script type="text/javascript">$(function() {$.extend({staticextend: function() {alert("This is a static extend function, called staticextend.")}});$.fn.extend({jqueryextend: function() {alert("all jquery object can call this function, and I am jqueryextend.")}});//以靜態(tài)方法的形式直接使用jquery本身的對象調(diào)用if ($.staticextend) {$.staticextend();} else {alert("There is not a function called staticextend in Jquery Object itself.");}//以靜態(tài)方法的形式直接調(diào)用jqueryextendif ($.jqueryextend) {$.jqueryextend();} else {alert("There is not a function called jqueryextend in Jquery Object itself. You must get wrong.");}//獲取一個jquery實(shí)例var jdiv = $(".idtest");//一個jquery實(shí)例本身包含了一個數(shù)組對象if (jdiv.length>1) {alert("we can see jdiv contains an array object!");}//如果jquery有多個,需要這樣調(diào)用。jdiv.each(function() {alert("I am "+ $(this).attr("id")); //這里不能直接使用this,因?yàn)閠his是htmlelement,沒有attr方法。if (jdiv.staticextend) {jdiv.staticextend();} else {alert("There is not a function called staticextend in jdiv ");}if (jdiv.jqueryextend) {jdiv.jqueryextend();} else {alert("There is not a function called jqueryextend in jdiv. You must get wrong.");}});});</script> </head> <body><div id="one" class="idtest"></div> <div id="two" class="idtest"></div> </body> </html>

如果擴(kuò)展jquery已經(jīng)存在的方法,比如拿jqgrid來說,如果你想修改某個方法,讓他在方法的執(zhí)行前后做些事情,這樣的話,我們可以這樣做:

var oldEditCell = $.fn.jqGrid.editCell; $.jgrid.extend({editCell: function (iRow,iCol, ed){var ret;// do someting beforeret = oldEditCell.call (this, iRow, iCol, ed);// do something afterreturn ret; // return original or modified results} });

在這里,我們覆蓋了原始的editCell方法,你可以在這個方法的前后做些事情。


轉(zhuǎn)載于:https://www.cnblogs.com/kevinhigher/archive/2011/10/15/2213759.html

總結(jié)

以上是生活随笔為你收集整理的实例说明扩展JQuery方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。