【发布时间】:2011-06-25 07:04:38
【问题描述】:
我正在为下拉菜单的一些常见任务编写插件。 Selected Index 方法需要返回一个值给我。我如何在插件中完成此操作,其中某些方法可能会或可能不会返回值?对于不返回值的方法,我想保持可链接性。
jQuery.fn.dropdownHelper = function(method) {
var methods = {
init: function(){ },
empty: function(){
if (this.tagName == 'SELECT')
this.options.length = 0;
},
selectedIndex: function(){
var index = this.selectedIndex;
if(index == 'undefined')
index = -1;
alert (index);
return index;
}
};
return this.each(function() {
// 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' );
});
};
【问题讨论】: