2014-11-21 56 views
0

我似乎無法訪問在點擊事件中創建的插件函數。 創建插件後,我創建另一個按鈕,通常會使用插件的外部功能,但似乎沒有辦法來解決功能。 當在document.ready事件中創建插件時,我沒有訪問該函數的問題。在事件內部創建插件時無法訪問jquery插件的外部函數

任何想法?

var pluginHolder = $('<div/>'); //just in case this was the issue 
$("body").on("click","#profile_plugin",function(e) 
{ 

pluginHolder = $('<div/>',{id:"coverCanvas"}); //Holding external button and plugin in a div 
abutton = $('<button/>',{class:"buttonis"}); 
plugin = $('<div/>'); 

plugin.testClass({ 
    c1:2, 
    c2:"ffffff", 
    c3:940, 
}); 
//works fine (plugin renders and fully functional) 

plugin.appendTo(pluginHolder); 
abutton.appendTo(pluginHolder); 
pluginHolder.appendTo($(".container"); //Works 

console.log(plugin.testoutput()); //Uncaught TypeError: undefined is not a function 
abutton.on("click",doStuff); 

function doStuff(e) 
{ 
    console.log(plugin.testoutput()); //Uncaught TypeError: undefined is not a function 
    console.log(plugin[0].testoutput()); //Uncaught TypeError: undefined is not a function 
} 

}); 
+0

您的代碼中沒有函數'testoutput'。 – 2014-11-21 21:23:55

+0

是的。我沒有在代碼中包含原型函數。 TestOutput()是原型類對象「TestClass」的測試函數, – skoumas 2014-11-25 11:29:59

回答

0

我找到了解決辦法,所以我將只是張貼在這裏以供將來參考:

var coverCanvasObj = null; 
var divContainer = $('<div/>'); 
coverCanvasObj = divContainer.testClass({ 
    option1:2, 
    option2:tru 
}); 

所以我只需要訪問渲染/ div容器外的TestClass對象。