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插件开发的更多相关文章

  1. JavaScript学习笔记(四)——jQuery插件开发与发布

    jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...

  2. JavaScript学习总结(四)——jQuery插件开发与发布

    jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...

  3. jQuery插件开发精品教程,让你的jQuery提升一个台阶

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  4. jquery插件开发

    jQuery是一个封装的很好的类,比如我们用语句$("#btn1") 会生成一个 jQuery类的实例. 一.jQuery插件开发注意要点 1.使用闭包,避免全局依赖,避免第三方破 ...

  5. jQuery插件开发(溢出滚动)

    声明:此程序仅针对手机端,简单的封装一个插件,意在记载插件的开发过程,如有错误及不足之处,还望即时指出. 移动开发的时候,我们经常会遇到滑动事件,众所周知手机端滑动主要依靠touch事件.最近接连遇到 ...

  6. 从零开始学jQuery插件开发

    http://www.w3cfuns.com/notes/19462/ec18ab496b4c992c437977575b12736c.html jQuery 最成功的地方,是它的可扩展性,通过吸引了 ...

  7. jquery插件开发继承了jQuery高级编程思路

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  8. jQuery插件开发(转)

    jQuery插件开发全解析 jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命 ...

  9. jQuery插件开发的两种方法及$.fn.extend的详解

    jQuery插件开发分为两种: 1 类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax(...),相当于静态方法. 开发扩展其方法时使用$.extend方法,即jQuery.ex ...

随机推荐

  1. 使用Jasypt对SpringBoot配置文件加密

    # **前言** 在日前安全形势越来越严重的情况下,让我意识到在项目中存在一个我们经常忽略的漏洞,那就是我们的项目的配置文件中配置信息的安全,尤其是数据库连接的用户名和密码的安全.所以这里我们就需要对 ...

  2. react学习(二)--元素渲染

    元素用来描述你在屏幕上看到的内容: const element = <h1>Hello, world</h1>; 与浏览器的 DOM 元素不同,React 当中的元素事实上是普 ...

  3. 【Kubernetes 系列一】Kubernetes 概述

    以下内容还可以通过 Google Slide 查看:https://docs.google.com/presentation/d/1eYP4bkVBojI_e6PqdpxIf0hvWO-JwAf-fy ...

  4. Python 环境管理

    Python 版本管理器:pyenv zsh 配置 # 安装 curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv ...

  5. SoapSerialization——手机号码归属地

    public class MainActivity extends AppCompatActivity { private EditText etNumber; private TextView tv ...

  6. Java - 手动解析不带引号的JSON字符串

    目录 1 需求说明 2 解析代码 2.1 实现思路 2.2 详细代码 2.3 测试样例 1 需求说明 项目中遇到了一批不带引号的类JSON格式的字符串: {Name:Heal,Age:20,Tag:[ ...

  7. 类spring ioc 泛型保留

    类spring ioc 泛型保留 什么是泛型擦除 Java并不会传递泛型类,举个直观的栗子: @Component public class BaseProvider<T> { publi ...

  8. random库的使用

    一.random库介绍 random库是使用随机数的Python标准库 伪随机数:采用梅森旋转算法生成的(伪)随机序列中元素 random库主要用于生成随机数 使用random库:import ran ...

  9. lombok 下的@Builder注解用法

    pom依赖 <dependency> <groupId>org.projectlombok</groupId>            <artifactId& ...

  10. 图表控件业界革命 -Arction新产品LightningChart JS 上市

    芬兰高科技企业Arction Ltd 在今年8月份推出了用于网页的数据可视化控件新解决方案—— LightningChart JS. 初次的基准测试表明,该产品为网页应用程序的数据可视化刷新了新的纪录 ...