[整理] 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 ...
随机推荐
- Vue小事例
login <!DOCTYPE html><html lang="ZH-cn"> <head> <meta charset="U ...
- JVM 内存模型概述
我们都知道,Java程序在执行前首先会被编译成字节码文件,然后再由Java虚拟机执行这些字节码文件从而使得Java程序得以执行.事实上,在程序执行过程中,内存的使用和管理一直是值得关注的问题.Java ...
- 《机器学习技法》---soft-margin SVM
1. soft-margin SVM的形式 其中ξn表示每个点允许的犯错程度(偏离margin有多远),但是犯错是有代价的,也就是目标函数里面要最小化的.c控制对犯错的容忍程度. 2. 推导soft ...
- python3虚拟环境中解决 ModuleNotFoundError: No module named '_ssl'
前提是已经安装了openssl 问题 当我在python3虚拟环境中导入ssl模块时报错,报错如下: (py3) [root@localhost Python-3.6.3]# python3 Pyth ...
- ZooKeeper实现读写锁
在上一篇文章,我们已经实现了分布式锁.今天更进一步,在分布式锁的基础之上,实现读写锁. 完整代码在 https://github.com/SeemSilly/codestory/tree/master ...
- 简单架构:反射实现抽象工厂+IDAL接口完全独立DAL
一.普通架构中存在的问题 StudentDB数据库,包含一张StudentInfoTB表,结构如下: s_id int primary key identity(1,1), s_name Nvarch ...
- Visual Studio 中 Build、Rebuild 、 Clean 之间的区别是什么?
今天翻看c-sharpcorner技术网站看到了这样一篇小记,标题为:What Is The Difference Between Build, Rebuild And Clean In Visual ...
- oauth2.0授权详解
学习oauth认证之前先回顾一下通过sessionid的会话过程 关于session与cookie的请戳:https://www.cnblogs.com/moran1992/p/10793748.ht ...
- 以后可得记住了--Python笔试面试题小结
1.字符串处理 将字符串中的数字替换成其两倍的值,例如: 修改前:"AS7G123m (d)F77k" 修改后:"AS14G246m (d)F154k" 个 ...
- 使用 Docker Compose 快速构建 TiDB 集群
本文档介绍如何在单机上通过 Docker Compose 快速一键部署一套 TiDB 测试集群.Docker Compose 可以通过一个 YAML 文件定义多个容器的应用服务,然后一键启动或停止. ...