1
我是jQuery新手,尤其是JavaScript。jQuery插件 - 在不同的父變量內部從外部訪問子值
我想創建一個jQuery插件,但有一些回調事件的困難。我想將回調事件包含到不同的函數中,這樣插件就會有點靈活。
下面是代碼:
(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_selectbox_open(param,event)
裏面,有開頭,在這裏我想提出一個回調事件的線,所以從外面初始化插件的時候,我可以正常訪問,這給了我一個JavaScript錯誤,告訴settings.onOpenBefore...
未定義。如何定義它?
在此先感謝
我'對不起啞巴,我只是不能得到它,我已經試過'; methods.init(settings.onOpenBefore.call(param,event));'和'methods.init(onOpenBefore.call(param,event));''和'methods.settings(init.onOpenBefore.call(param,event ));'但是沒有一個能起作用。你能多描述一下嗎?謝謝 – aspirinemaga 2012-07-25 11:10:23
你應該可以將它們稱爲'methods.settings.onOpenBefore(...);' – techfoobar 2012-07-25 14:28:09
這裏是我處理它們的方式:我在'var methods = {}之前創建了'var settings:null'', ...',所以這將是一個全局變量,然後在'var methods = {init:function(options){....' 謝謝@techfoobar,指向我在正確的方向。 – aspirinemaga 2012-07-25 14:35:24