jQuery插件开发全解析,类级别与对象级别开发
添加一个全局函数,我们只需如下定义:
|
1
2
3
|
jQuery.foo = function() {alert('This is a test. This is only a test.');}; |
添加多个全局函数,可采用如下定义:
|
1
2
3
4
5
6
7
|
jQuery.foo = function() {alert('This is a test. This is only a test.');};jQuery.bar = function(param) {alert('This function takes a parameter, which is "' + param + '".');};调用时和一个函数的一样的:jQuery.foo();jQuery.bar();或者$.foo();$.bar('bar'); |
1.3 使用jQuery.extend(object);
|
1
2
3
4
5
6
7
8
|
jQuery.extend({ foo: function() { alert('This is a test. This is only a test.'); }, bar: function(param) { alert('This function takes a parameter, which is "' + param +'".'); } }); |
|
1
2
3
4
5
6
7
8
9
10
11
|
jQuery.myPlugin = { foo:function() { alert('This is a test. This is only a test.'); }, bar:function(param) { alert('This function takes a parameter, which is "' + param + '".');} };采用命名空间的函数仍然是全局函数,调用时采用的方法:$.myPlugin.foo(); $.myPlugin.bar('baz'); |
|
1
2
3
4
5
6
7
|
(function($){ $.fn.extend({ pluginName:function(opt,callback){ // Our plugin implementation code goes here. } }) })(jQuery); |
形式2:
|
1
2
3
4
5
|
(function($) { $.fn.pluginName = function() { // Our plugin implementation code goes here. }; })(jQuery); |
|
1
2
3
4
5
|
$.fn.hilight = function() { // Our plugin implementation code goes here. }; 我们的插件通过这样被调用:$('#myDiv').hilight(); |
2.2 接受options参数以控制插件的行为
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// plugin definition$.fn.hilight = function(options) { var defaults = { foreground: 'red', background: 'yellow' }; // Extend our default options with those provided. var opts = $.extend(defaults, options); // Our plugin implementation code goes here.};我们的插件可以这样被调用:$('#myDiv').hilight({ foreground: 'blue'}); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// plugin definition$.fn.hilight = function(options) { // Extend our default options with those provided. // Note that the first arg to extend is an empty object - // this is to keep from overriding our "defaults" object. var opts = $.extend({}, $.fn.hilight.defaults, options); // Our plugin implementation code goes here.};// plugin defaults - added as a property on our plugin function$.fn.hilight.defaults = { foreground: 'red', background: 'yellow'}; 现在使用者可以包含像这样的一行在他们的脚本里://这个只需要调用一次,且不一定要在ready块中调用$.fn.hilight.defaults.foreground = 'blue';接下来我们可以像这样使用插件的方法,结果它设置蓝色的前景色:$('#myDiv').hilight(); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// plugin definition$.fn.hilight = function(options) { // iterate and reformat each matched element return this.each(function() { var $this = $(this); // ... var markup = $this.html(); // call our format function markup = $.fn.hilight.format(markup); $this.html(markup); });};// define our format function$.fn.hilight.format = function(txt) {return '<strong>' + txt + '</strong>';}; |
考虑到这个篇文章中我们建立的无用的插件,你也许想知道究竟什么时候这些会有用。一个真实的例子是Cycle插件.这个Cycle插件是一个滑动显示插件,他能支持许多内部变换作用到滚动,滑动,渐变消失等。但是实际上,没有办法定义也许会应用到滑动变化上每种类型的效果。那是这种扩展性有用的地方。 Cycle插件对使用者暴露"transitions"对象,使他们添加自己变换定义。插件中定义就像这样:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
(function($) { // plugin definition $.fn.hilight = function(options) { debug(this); // ... }; // private function for debugging function debug($obj) { if (window.console && window.console.log) window.console.log('hilight selection count: ' + $obj.size()); };// ...})(jQuery); |
|
1
2
3
4
5
6
7
8
9
|
$.fn.hilight = function(options) { // ... // build main options before element iteration var opts = $.extend({}, $.fn.hilight.defaults, options); return this.each(function() { var $this = $(this); // build element specific options var o = $.meta ? $.extend({}, opts, $this.data()) : opts; //... |
|
1
2
3
4
5
6
7
8
9
10
11
|
<div .="hilight { background: 'red', foreground: 'white' }"> Have a nice day!</div><div .="hilight { foreground: 'orange' }"> Have a nice day!</div><div .="hilight { background: 'green' }"> Have a nice day!</div>现在我们能高亮哪些div仅使用一行脚本:$('.hilight').hilight(); |
2.7 整合
下面使我们的例子完成后的代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
// 创建一个闭包(function($) { // 插件的定义 $.fn.hilight = function(options) { debug(this); // build main options before element iteration var opts = $.extend({}, $.fn.hilight.defaults, options); // iterate and reformat each matched element return this.each(function() { $this = $(this); // build element specific options var o = $.meta ? $.extend({}, opts, $this.data()) : opts; // update element styles $this.css({ backgroundColor: o.background, color: o.foreground }); var markup = $this.html(); // call our format function markup = $.fn.hilight.format(markup); $this.html(markup); }); }; // 私有函数:debugging function debug($obj) { if (window.console && window.console.log) window.console.log('hilight selection count: ' + $obj.size()); }; // 定义暴露format函数 $.fn.hilight.format = function(txt) { return '<strong>' + txt + '</strong>'; }; // 插件的defaults $.fn.hilight.defaults = { foreground: 'red', background: 'yellow' };// 闭包结束})(jQuery); |
jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。
jQuery插件开发全解析,类级别与对象级别开发的更多相关文章
- jQuery插件开发全解析
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- (转)jQuery插件开发全解析
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- jQuery插件开发全解析[转]
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- 转:jQuery插件开发全解析
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- jQuery插件开发全解析<转>
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- jQuery 插件开发全解析
jQuery插件的开发包含两种: 一种是类级别的插件开发,即给jQuery加入新的全局函数,相当于给jQuery类本身加入方法.jQuery 的全局函数就是属于jQuery命名空间的函数,还有一种是对 ...
- jQuery插件开发——全屏切换插件
这个插件包含三个部分:HTML结构.CSS代码和JS代码. HTML结构是固定的,结构如下: <!--全屏滚动--> <div class="fullpage-contai ...
- jQuery Ajax 全解析
转自:http://www.cnblogs.com/qleelulu/archive/2008/04/21/1163021.html 本文地址: jQuery Ajax 全解析 本文作者:QLeelu ...
- jQuery Ajax 全解析(转载)
本文地址: jQuery Ajax 全解析 本文作者:QLeelulu 转载请标明出处! jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写Java ...
随机推荐
- SQLServer学习笔记<>.基础知识,一些基本命令,单表查询(null top用法,with ties附加属性,over开窗函数),排名函数
Sqlserver基础知识 (1)创建数据库 创建数据库有两种方式,手动创建和编写sql脚本创建,在这里我采用脚本的方式创建一个名称为TSQLFundamentals2008的数据库.脚本如下: ...
- win7下80端口被(Pid=4)占用的解决方法
首先介绍一种网上普遍的方法,就是查找占据80端口的进程,然后关闭它就行了. 1.运行cmd,然后输入netstat -a -n -o,回车:2.查看开头几行包含0.0.0.0:80的那一行最后的pid ...
- samba服务器源码安装(非rpm)
首先我们创建一个文档,边安装配置samba,边写教程. 从www.samba.org下载samba最新源码包,我下载的是samba-3.0.7.tar.gz,把它放在我的目录的中/root/lova/ ...
- ACM题目————玩转二叉树
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出 ...
- 20150618_Andriod _set Dialog_弹出式菜单
参考地址: http://blog.csdn.net/zhyl8157121/article/details/8169172 http://blog.csdn.ne ...
- andriod 新建 Activity_ Form (详细设置)
参考: Starting Another Activity 去创建Activity New->Other->Android->Android Activity->BlankAc ...
- Ugly Numbers
Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...
- Codeforces Round #260 (Div. 2) C
Description Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. On ...
- CssSpritesGenerator使用
最近一直在看CSS 的东西,在优化之路是哪个继续前进. 什么是css sprites css sprites直译过来就是CSS精灵,但是这种翻译显然是不够的,其实就是通过将多个图片融合到一副图里面,然 ...
- zh-cn en-uk、zh-tw表示语言(文化)代码与国家地区对照表(最全的各国地区对照表)
af 公用荷兰语 af-ZA 公用荷兰语 - 南非 sq 阿尔巴尼亚 sq-AL 阿尔巴尼亚 -阿尔巴尼亚 ar 阿拉伯语 ar-DZ 阿拉伯语 -阿尔及利亚 ar-BH 阿拉伯语 -巴林 ar-EG ...