带有api的Jquery插件
如何从插件中检索API变量。根据Jquery编写文档,以下是插件结构:带有api的Jquery插件,jquery,api,plugins,Jquery,Api,Plugins,如何从插件中检索API变量。根据Jquery编写文档,以下是插件结构: (function( $ ){ var methods = { init : function( options ) { return this.each(function() { var settings = { 'height' : 10, 'width'
(function( $ ){
var methods = {
init : function( options ) {
return this.each(function() {
var settings = {
'height' : 10,
'width' : 10
};
if ( options ) { $.extend( settings, options ); }
$('<div>', {
css : {
height : settings.height,
width : settings.width,
}
}).appendTo(this);
});
}
};
$.fn.myPlugin = function( method ) {
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.element' );
}
};
})( jQuery );
在properties中,我会有一个函数返回Div的高度和宽度。在这个设计中,我将如何创建properties变量
谢谢你会摆脱
return this.each(function() { ... });
…并让您的插件返回诸如
return {
'properties': {
'width': calculatedWidth,
'height': calculatedHeight
}
}
然而,这样做将意味着你无法链接其他方法。你将摆脱
return this.each(function() { ... });
…并让您的插件返回诸如
return {
'properties': {
'width': calculatedWidth,
'height': calculatedHeight
}
}
但是,这样做意味着你不能链接其他方法。谢谢,事实上我的插件中有更多的方法。这只是一个例子。理想情况下,我希望只有创建元素的init方法,然后我会像这样执行所有其他操作<代码>变量元素=$('div').myPlugin();元素destroy();元素。getProperties()代码>谢谢,事实上我的插件中有更多的方法。这只是一个例子。理想情况下,我希望只有创建元素的init方法,然后我会像这样执行所有其他操作<代码>变量元素=$('div').myPlugin();元素destroy();元素。getProperties()代码>