总结jQuery插件开发中的一些要点

总结jQuery插件开发中的一些要点

基础

1、jQuery插件开发主要使用如下两个方法:

1.1、添加静态方法

jQuery.extend(object);

为扩展jQuery本身,为类添加新的方法,可以理解文添加静态方法。

$.extend({
  addMethod : function(a, b){return a b;} // $.addMethod(1, 2); //return 3
});

1.2、添加成员方法

jQuery.fn.extend(object);
jQuery.fn = jQuery.prototype

给jQuery对象添加方法,对jQuery.prototype进行扩展,为jQuery类添加成员方法:

$.fn.extend({
getInputText:function(){
$(this).click(function(){
alert($(this).val());
});
}
});

$(“#username”).getInputText();

2、一个通用的框架:

以下是一个通用的框架:

(function($){
$.fn.yourPluginName = function(options){
//各种属性和参数

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

this.each(function(){
//插件的实现代码

});
};
})(jQuery);

关于

$.extend(defaults, options);

就是通过合并defaults和options来扩展defaults,实现插件默认参数的功能。

实践

1、声明插件名称:

添加一个函数到jQuery.fn(jQuery.prototype)对象,该函数的名称就是你的插件名称:

jQuery.fn.myPlugin = function() {

// Do your awesome plugin stuff here

};

在命名不与jQuery其他函数冲突的情况,可以使用闭包的方式使用$符号:

(function( $ ) {
$.fn.myPlugin = function() {

// Do your awesome plugin stuff here

};
})( jQuery );

2、插件中的上下文:

jQuery接收一个回调,该回调本身就指向了dom,直接使用this就相当于我们平时的$(this)的情况:

(function( $ ){

$.fn.myPlugin = function() {

// theres no need to do $(this) because
// “this” is already a jquery object

// $(this) would be the same as $($(#element));

this.fadeIn(normal, function(){

// the this keyword is a DOM element

});

};
})( jQuery );
$(#element).myPlugin();

下面是一个简单的jQuery插件,实现获取所有div的最大高度:

(function( $ ){

$.fn.maxHeight = function() {

var max = 0;

this.each(function() {
max = Math.max( max, $(this).height() );
});

return max;
};
})( jQuery );
var tallest = $(div).maxHeight(); // Returns the height of the tallest div

3、维护链接性:

前面的示例返回一个整数值最高的div,但是通常的意图仅在某种程度上是仅修改插件的元素集合,并将它们传递到下一个链中的方法。这样的jQuery的设计优雅的地方。所以保持链接性放到一个插件,您必须确保您的插件返回这个关键字。

(function( $ ){

$.fn.lockDimensions = function( type ) {

return this.each(function() {

var $this = $(this);

if ( !type || type == width ) {
$this.width( $this.width() );
}

if ( !type || type == height ) {
$this.height( $this.height() );
}

});

};
})( jQuery );
$(div).lockDimensions(width).css(color, red);

因为插件返回this关键字的范围,它维护链接性,jQuery可以继续利用jQuery方法,如. css。所以,如果你的插件不返回一个内在价值,你应该总是返回this。

4、参数的传递,Defaults和Options:

(function( $ ){

$.fn.tooltip = function( options ) {

// Create some defaults, extending them with any options that were provided
var settings = $.extend( {
location : top,
background-color : blue
}, options);

return this.each(function() {

// Tooltip plugin code here

});

};
})( jQuery );
$(div).tooltip({
location : left
});

通过$.extend合并默认参数和调用者传入的参数。

5、命名空间:

正确的命名空间在插件开发中是一个非常重要的部分。正确的命名空间,可以确保你的插件将有一个很低的几率在同一个页面上被他插件或代码覆盖。

在任何情况下都不应该在一个插件的jQuery.fn对象中声称多个名称空间。

(function( $ ){

$.fn.tooltip = function( options ) {
// THIS
};
$.fn.tooltipShow = function( ) {
// IS
};
$.fn.tooltipHide = function( ) {
// BAD
};
$.fn.tooltipUpdate = function( content ) {
// !!!
};

})( jQuery );

你应该收集所有的方法到一个对象化字面,并且通过方法的字面值进行调用:

(function( $ ){

var methods = {
init : function( options ) {
// THIS
},
show : function( ) {
// IS
},
hide : function( ) {
// GOOD
},
update : function( content ) {
// !!!
}
};

$.fn.tooltip = function( method ) {

// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === object || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( Method method does not exist on jQuery.tooltip );
}

};

})( jQuery );

// calls the init method
$(div).tooltip();

// calls the init method
$(div).tooltip({
foo : bar
});
// calls the hide method
$(div).tooltip(hide);
// calls the update method
$(div).tooltip(update, This is the new tooltip content!);

这种类型的方法封装和体系结构在jQuery插件社区中是一个标准,它适用于无数的插件,包括jQueryUI插件和小部件。

6、Events:

Bind方法允许通过命名空间的方式绑定事件,如果你的插件绑定了事件,可以通过命名空间的方式,在解除绑定事件时,你也可以这样做,来防止影响到其他的事件。可以通过“.

© 版权声明

相关文章

暂无评论

none
暂无评论...