[整理] jQuery插件开发
1、类级别的插件开发
类级别的插件开发,可似为给jQuery类添加方法,调用方式:$.你的方法(),如:$.ajax() 函数。
1.1、给jQuery类添加方法
$.alertMsg = function(msg){
alert("alertMsg : " + msg);
};
// 调用
// $.alertMsg("hello");
1.2、给jQuery类添加带命名空间的方法
$.myPlugin = {
alertMsg : function(msg){
alert("myPlugin.alertMsg : " + msg);
},
};
// 调用
// $.myPlugin.alertMsg("hello");
1.3、使用 jQuery.extend 给jQuery类添加方法
$.extend({
alertMsg2 : function(msg){
alert("alertMsg2 : " + msg);
},
// 调用
// $.alertMsg2("hello");
myPlugin2 : {
alertMsg : function(msg){
alert("myPlugin2.alertMsg : " + msg);
},
},
// 调用
// $.myPlugin2.myPlugin2("hello");
});
2、对象级别的插件开发
对象级别的插件开发,可似为给jQuery对象添加方法,调用方式:$(对象).你的方法(),如:$("body").css() 函数。
2.1、给jQuery对象添加方法
$.fn.alertText = function(msg){
alert("alertText : " + this.text() + " , " + msg);
};
// 调用
// $(elem).alertText("hello");
2.2、给jQuery对象添加带名命空间的方法
$.fn.myPlugin = {
alertText : function(msg){
// this 为 myPlugin 对象,要获取事件源,使用event.target
alert("myPlugin.alertText : " + $(event.target).text() + " , " + msg);
},
};
// 调用
// $(elem).myPlugin.alertText("hello");
$.fn.myPlugin2 = function(method){
var methods = {
init : function(){
alert("myPlugin2.init");
},
alertText : function(msg){
alert("myPlugin2.alertText : " + this.text() + " , " + msg);
},
};
if(methods[method]){
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}else{
return methods.init();
}
};
// 调用
// $(elem).myPlugin2(); // $(elem).myPlugin2("init");
// $(elem).myPlugin2("alertText", "hello");
2.3、 使用 jQuery.fn.extend 给jQuery对象添加方法
$.fn.extend({
alertText2 : function(msg){
alert("alertText2 : " + this.text() + " , " + msg);
},
// 调用
// $(elem).alertText2("hello");
myPlugin3 : {
alertText : function(msg){
// this 为 myPlugin 对象,要获取事件源,使用event.target
alert("myPlugin3.alertText : " + $(event.target).text() + " , " + msg);
},
},
// 调用
// $(elem).myPlugin3.alertText("hello");
myPlugin4 : function(method){
var methods = {
init : function(){
alert("myPlugin4.init");
},
alertText : function(msg){
alert("myPlugin4.alertText : " + this.text() + " , " + msg);
},
};
if(methods[method]){
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}else{
return methods.init();
}
}
// 调用
// $(elem).myPlugin4(); // $(elem).myPlugin4("init");
// $(elem).myPlugin4("alertText", "hello");
});
[整理] jQuery插件开发的更多相关文章
- JavaScript学习笔记(四)——jQuery插件开发与发布
jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...
- JavaScript学习总结(四)——jQuery插件开发与发布
jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...
- jQuery插件开发精品教程,让你的jQuery提升一个台阶
要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...
- jquery插件开发
jQuery是一个封装的很好的类,比如我们用语句$("#btn1") 会生成一个 jQuery类的实例. 一.jQuery插件开发注意要点 1.使用闭包,避免全局依赖,避免第三方破 ...
- jQuery插件开发(溢出滚动)
声明:此程序仅针对手机端,简单的封装一个插件,意在记载插件的开发过程,如有错误及不足之处,还望即时指出. 移动开发的时候,我们经常会遇到滑动事件,众所周知手机端滑动主要依靠touch事件.最近接连遇到 ...
- 从零开始学jQuery插件开发
http://www.w3cfuns.com/notes/19462/ec18ab496b4c992c437977575b12736c.html jQuery 最成功的地方,是它的可扩展性,通过吸引了 ...
- jquery插件开发继承了jQuery高级编程思路
要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...
- jQuery插件开发(转)
jQuery插件开发全解析 jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命 ...
- jQuery插件开发的两种方法及$.fn.extend的详解
jQuery插件开发分为两种: 1 类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax(...),相当于静态方法. 开发扩展其方法时使用$.extend方法,即jQuery.ex ...
随机推荐
- pythonday05数据类型(三)
---恢复内容开始--- 今日内容 1.字典 2.强制转换 3.习题讲解 1.字典 帮助用户去表示一个事物的信息(事物是有多个属性). info = {"name":'刘伟达',' ...
- 使用appscan安全扫描问题以及解决办法
最近在做安全扫描,把遇到的一些问题以及一些解决方法记录下,以备后用. 扫描软件: IBM Security AppScan Standard 规则: 17441 1. 已解密的登录请求 (高) - ...
- SIMBOSS:物联网业务如何应用领域驱动设计?
前言 在这个万物互联的时代,物联网业务蓬勃发展,但也瞬息万变,对于开发人员来说,这是一种挑战,但也是一种“折磨”. 在业务发展初期,因为时间有限,我们一般会遵循“小步快跑,迭代试错”的原则进行业务开发 ...
- RecyclerView实现混合布局
PS:好长时间不写博客了,起初是不知道写些什么,后来接触了到了很多东西,原本看似简单的东西,背后都隐藏着巨大的秘密,想handler的使用,一般情况下会引起内存泄漏问题,想着找到方法结局不就得了吗,可 ...
- peewee
字段查看http://docs.peewee-orm.com/en/latest/peewee/models.html#fields 方法使用https://blog.csdn.net/qq_3962 ...
- 基于HttpClient4.5.1实现Http访问工具类
本工具类基于httpclient4.5.1实现 <dependency> <groupId>org.apache.httpcomponents</groupId> ...
- excache.xml作用
name:缓存名称. maxElementsInMemory:缓存最大个数. eternal:对象是否永久有效,一但设置了,timeout将不起作用. timeToIdleSeconds:设置对象在失 ...
- java 路径问题
java路径存在两种写法"/"和"\\" String path="D:\\1.txt"; String path1="D:/1. ...
- 三维动画形变算法(Laplacian-Based Deformation)
网格上顶点的Laplace坐标(均匀权重)定义为:,其中di为顶点vi的1环邻域顶点数. 网格Laplace坐标可以用矩阵形式表示:△=LV,其中,那么根据网格的Laplace坐标通过求解稀疏线性方程 ...
- 全球DEM数据资源下载
想找有海底地形的全球DEM数据作为三维地球展示用,发现很多都是只有陆地DEM而不带海底的,而且还需要通过Web页面进行选择然后数据下载. 找到一个学校的Ftp可以直接下载数据集,特别是这篇文章几乎汇集 ...