Javascript jQuery插件-从外部访问不同父变量中的子值
我不熟悉jQuery,尤其是JavaScript 我正在尝试创建jQuery插件,但在回调事件方面遇到了一些困难。我想将回调事件包含到不同的函数中,这样插件就有点灵活了 代码如下:Javascript jQuery插件-从外部访问不同父变量中的子值,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我不熟悉jQuery,尤其是JavaScript 我正在尝试创建jQuery插件,但在回调事件方面遇到了一些困难。我想将回调事件包含到不同的函数中,这样插件就有点灵活了 代码如下: (function($, undefined){ var methods = { init: function(options){ var settings = $.extend({ // Default settings
(function($, undefined){
var methods = {
init: function(options){
var settings = $.extend({
// Default settings
'fadeInOutSpeed' : 100,
// the rest of default settings ...
// Callback events
onOpenBefore : function(){},
onOpen : function(){},
onOpenedCloseBefore : function(){}
// the rest of the callback events...
}, options);
// code goes here...
return this.each(function(){
// code goes here
});
},
destroy: function(){
// code goes here
},
open: function(){
console.info('method open accessed');
},
close: function(){
console.info('method close accessed');
},
update: function(){
console.info('method update accessed');
}
};
function gui_selectbox_open(param, event){
// HOW TO PUT A CORRECT CALLBACK CODE HERE ?
settings.onOpenBefore.call(param, event);
// function code goes here
settings.onOpenAfter.call(param, event);
};
在函数gui\u selectbox\u open(param,event)
的开头有一行,我想在这里放置一个回调事件,这样我可以在从外部初始化插件时正确地访问它,这会给我一个JavaScript错误,告诉我settings.onOpenBefore…
没有定义。如何定义它
提前感谢您的变量
设置
被定义为方法
对象中的init()
函数的本地变量。要在外部访问它,您可以在init
函数外部定义它,并将其作为methods.settings.yourMethod(…)
i、 e
变量
settings
被定义为methods
对象中的init()
函数的本地变量。要在外部访问它,您可以在init
函数外部定义它,并将其作为methods.settings.yourMethod(…)
i、 e
很抱歉我太笨了,我就是搞不懂,我试过
methods.init(settings.onOpenBefore.call(param,event))
和方法.init(onOpenBefore.call(param,event))
和方法.设置(init.onOpenBefore.call(param,event))代码>但它们都不起作用。你能再描述一下吗?谢谢,您应该能够将它们称为methods.settings.onOpenBefore(…)代码>以下是我如何处理它们的:我在var-methods={…
之前创建了一个var-settings:null
,因此它将是一个全局变量,然后在var-methods={init:function(选项)内部创建{……
感谢@techfoobar为我指明了正确的方向。我很高兴。很高兴它帮了我的忙。:)很抱歉我太笨了,我就是弄不懂,我已经尝试了方法.init(settings.onOpenBefore.call(param,event));
和方法.init(onOpenBefore.call)(param,event));
和方法.settings(init.onOpenBefore.call)(param,event));
但它们都不起作用。你能再多描述一下吗?谢谢你应该能够将它们称为方法。设置。onOpenBefore(…);
以下是我如何处理它们的:我在var方法={…
,因此它将是一个全局变量,然后在var方法中={init:function(options){…
感谢@techfoobar为我指明了正确的方向。我很高兴。很高兴它有所帮助。:)
var methods = {
settings: null,
init: function(options){
...