您的位置:首页 > Web前端 > JQuery

jQuery插件的开发

2015-07-28 14:16 645 查看
jQuery插件开发方式主要有三种:

通过$.extend()来扩展jQuery

通过$.fn 向jQuery添加新的方法

通过$.widget()应用jQuery UI的部件工厂方式创建

通常我们使用第二种方法来进行简单插件开发,说简单是相对于第三种方式。第三种方式是用来开发更高级jQuery部件的,该模式开发出来的部件带有很多jQuery内建的特性,比如插件的状态信息自动保存,各种关于插件的常用方法等,非常贴心,这里不细说。

而第一种方式又太简单,仅仅是在jQuery命名空间或者理解成jQuery身上添加了一个静态方法而以。所以我们调用通过$.extend()添加的函数时直接通过$符号调用($.myfunction())而不需要选中DOM元素($('#example').myfunction())。请看下面的例子

$.extend({

sayHello: function(name) {

console.log('Hello,' + (name ? name : 'Dude') + '!');

}

})

$.sayHello(); //调用

$.sayHello('Wayou'); //带参调用

$.extend({

log: function(message) {

var now = new Date(),

y = now.getFullYear(),

m = now.getMonth() + 1, //!JavaScript中月分是从0开始的

d = now.getDate(),

h = now.getHours(),

min = now.getMinutes(),

s = now.getSeconds(),

time = y + '/' + m + '/' + d + ' ' + h + ':' + min + ':' + s;

console.log(time + ' My App: ' + message);

}

})

$.log('initializing...'); //调用

下面我们就来看第二种方式的jQuery插件开发。

基本格式:

$.fn.pluginName = function() {

//your code goes here

}

将页面上所有链接颜色转成红色,则可以这样写这个插件:

$.fn.myPlugin = function() {

//在这里面,this指的是用jQuery选中的元素

//example :$('a'),则this=$('a')

this.css('color', 'red');

}

在上面插件代码中,我们在this身上调用jQuery的css()方法,也就相当于在调用 $('a').css()。

现在我们要在每个链接显示链接的真实地址,首先通过each遍历所有a标签,然后获取href属性的值再加到链接文本后面。

更改后我们的插件代码为:

$.fn.myPlugin = function() {

//在这里面,this指的是用jQuery选中的元素

this.css('color', 'red');

this.each(function() {

//对每个元素进行操作

$(this).append(' ' + $(this).attr('href'));

}))

}

支持链式调用只需return一下即可

$.fn.myPlugin = function() {

//在这里面,this指的是用jQuery选中的元素

this.css('color', 'red');

return this.each(function() {

//对每个元素进行操作

$(this).append(' ' + $(this).attr('href'));

}))

}

让插件接收参数

$.fn.myPlugin = function(options) {

var defaults = {

'color': 'red',

'fontSize': '12px'

};

var settings = $.extend(defaults, options);

return this.css({

'color': settings.color,

'fontSize': settings.fontSize

});

}

调用的时候指定颜色,字体大小未指定,会运用插件里的默认值12px。

$('a').myPlugin({

'color': '#2C9929'

});

同时指定颜色与字体大小:

$('a').myPlugin({

'color': '#2C9929',

'fontSize': '20px'

});

保护好默认参数

一个好的做法是将一个新的空对象做为$.extend的第一个参数,defaults和用户传递的参数对象紧随其后,这样做的好处是所有值

被合并到这个空对象上,保护了插件里面的默认值。

$.fn.myPlugin = function(options) {

var defaults = {

'color': 'red',

'fontSize': '12px'

};

var settings = $.extend({},defaults, options);//将一个空对象做为第一个参数

return this.css({

'color': settings.color,

'fontSize': settings.fontSize

});

}

面向对象的插件开发

//定义Beautifier的构造函数

var Beautifier = function(ele, opt) {

this.$element = ele,

this.defaults = {

'color': 'red',

'fontSize': '12px',

'textDecoration':'none'

},

this.options = $.extend({}, this.defaults, opt)

}

//定义Beautifier的方法

Beautifier.prototype = {

beautify: function() {

return this.$element.css({

'color': this.options.color,

'fontSize': this.options.fontSize,

'textDecoration': this.options.textDecoration

});

}

}

//在插件中使用Beautifier对象

$.fn.myPlugin = function(options) {

//创建Beautifier的实体

var beautifier = new Beautifier(this, options);

//调用其方法

return beautifier.beautify();

}

$(function() {

$('a').myPlugin({

'color': '#2C9929',

'fontSize': '20px'

});

})

用自调用匿名函数包裹你的代码

(function() {

//定义Beautifier的构造函数

var Beautifier = function(ele, opt) {

this.$element = ele,

this.defaults = {

'color': 'red',

'fontSize': '12px',

'textDecoration': 'none'

},

this.options = $.extend({}, this.defaults, opt)

}

//定义Beautifier的方法

Beautifier.prototype = {

beautify: function() {

return this.$element.css({

'color': this.options.color,

'fontSize': this.options.fontSize,

'textDecoration': this.options.textDecoration

});

}

}

//在插件中使用Beautifier对象

$.fn.myPlugin = function(options) {

//创建Beautifier的实体

var beautifier = new Beautifier(this, options);

//调用其方法

return beautifier.beautify();

}

})();

将系统变量以变量形式传递到插件内部

var foo=function(){

//别人的代码

}//注意这里没有用分号结尾

//开始我们的代码。。。

(function(){

//我们的代码。。

alert('Hello!');

})();

本来别人的代码也正常工作,只是最后定义的那个函数没有用分号结尾而以,然后当页面中引入我们的插件时,报错了,我们的代码无法正常执行。

原因是我们用来充当自调用匿名函数的第一对括号与上面别人定义的函数相连,因为中间没有分号嘛,总之我们的代码无法正常解析了,所以报错。

所以好的做法是我们在代码开头加一个分号,这在任何时候都是一个好的习惯。

var foo=function(){

//别人的代码

}//注意这里没有用分号结尾

//开始我们的代码。。。

;(function(){

//我们的代码。。

alert('Hello!');

})();

同时,将系统变量以参数形式传递到插件内部也是个不错的实践。

当我们这样做之后,window等系统变量在插件内部就有了一个局部的引用,可以提高访问速度,会有些许性能的提升

最后我们得到一个非常安全结构良好的代码:

;(function($,window,document,undefined){

//我们的代码。。

//blah blah blah...

})(jQuery,window,document);

而至于这个undefined,稍微有意思一点,为了得到没有被修改的undefined,我们并没有传递这个参数,但却在接收时接收了它,因为实际并没有传,所以‘undefined’那个位置接收到的就是真实的'undefined'了。是不是有点hack的味道,值得细细体会的技术,当然不是我发明的,都是从前人的经验中学习。

所以最后我们的插件成了这样:

;(function($, window, document,undefined) {

//定义Beautifier的构造函数

var Beautifier = function(ele, opt) {

this.$element = ele,

this.defaults = {

'color': 'red',

'fontSize': '12px',

'textDecoration': 'none'

},

this.options = $.extend({}, this.defaults, opt)

}

//定义Beautifier的方法

Beautifier.prototype = {

beautify: function() {

return this.$element.css({

'color': this.options.color,

'fontSize': this.options.fontSize,

'textDecoration': this.options.textDecoration

});

}

}

//在插件中使用Beautifier对象

$.fn.myPlugin = function(options) {

//创建Beautifier的实体

var beautifier = new Beautifier(this, options);

//调用其方法

return beautifier.beautify();

}

})(jQuery, window, document);

一个安全,结构良好,组织有序的插件编写完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: