jquery插件开发及 jquery自定义函数
来源:互联网 发布:免编程2d游戏引擎 编辑:程序博客网 时间:2024/05/01 12:45
jQuery插件的开发包括两种:
一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法。jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级别的插件开发,即给jQuery对象添加方法。下面就两种函数的开发做详细的说明。
1、类级别的插件开发
类级别的插件开发最直接的理解就是给jQuery类添加类方法,可以理解为添加静态方法。典型的例子就是$.AJAX()这个函数,将函数定义于jQuery的命名空间中。关于类级别的插件开发可以采用如下几种形式进行扩展:
1.1
添加一个全局函数,我们只需如下定义:
- jQuery.foo
= function() { - alert('This
is );a test. This is only a test.' - };
1.2
添加多个全局函数,可采用如下定义:
- 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({
- 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.4
虽然在jQuery命名空间中,我们禁止使用了大量的javaScript函数名和变量名。但是仍然不可避免某些函数或变量名将于其他jQuery插件冲突,因此我们习惯将一些方法封装到另一个自定义的命名空间。
- 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');
通过这个技巧(使用独立的插件名),我们可以避免命名空间内函数的冲突。
2、对象级别的插件开发
对象级别的插件开发需要如下的两种形式:、
形式1:
- (function($){
- $.fn.extend({
- pluginName:function(opt,callback){
-
// Our plugin implementation code goes here. - }
- })
- })(jQuery);
形式2:
- (function($)
{ - $.fn.pluginName
= function() { -
// Our plugin implementation code goes here. - };
- })(jQuery);
2.1
这是一个单一插件的脚本。如果你的脚本中包含多个插件,或者互逆的插件(例如:
- $.fn.hilight
= function() { -
// Our plugin implementation code goes here. - };
- 我们的插件通过这样被调用:
- $('#myDiv').hilight();
但是如果我们需要分解我们的实现代码为多个函数该怎么办?有很多原因:设计上的需要;这样做更容易或更易读的实现;而且这样更符合面向对象。
2.2
让我们为我们的插件添加功能指定前景色和背景色的功能。我们也许会让选项像一个options对象传递给插件函数。例如:
- //
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' - });
- jquery插件开发及 jquery自定义函数
- jquery插件开发及 jquery自定义函数
- jquery插件开发及 jquery自定义函数
- 【jQuery】jQuery自定义插件开发
- jquery 自定义插件开发
- jquery 自定义插件开发
- jQuery插件开发及jQuery.extend函数详解和jQuery.fn与jQuery.prototype区别
- jQuery插件开发及jQuery.extend函数详解和jQuery.fn与jQuery.prototype区别
- jQuery插件开发及jQuery.extend函数详解和jQuery.fn与jQuery.prototype区别
- jquery 自定义插件开发整理
- 根据JQuery开发自定义插件。
- jquery自定义插件开发总结
- [ jQuery ] jquery 自定义插件!
- jQuery自定义插件开发基础1——jQuery实用函数的实现
- jQuery自定义插件开发基础1——jQuery实用函数的实现 .
- jQuery自定义插件开发基础1——jQuery实用函数的实现
- jquery开发自定义的插件总结
- jquery开发自定义的插件总结
- RFC 3489 - STUN
- unexcepted end of input
- sudo一个小时内生效
- 对敏捷方法的一些简单认识
- IOS推送
- jquery插件开发及 jquery自定义函数
- XCode 快捷键, MAC 快捷键
- linux的ulimit各种限制之深入分析
- 在VC的界面里实现最简单的日志显示功能
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型
- 通过ContentProvider得到系统的音频,视频等
- flash builder 4.7 #1014: 无法找到类 mx.core::BitmapAsset解决办法
- Velocity语法大全
- 如何能让Java生成复杂Word文档