2013-02-20 61 views
0

根據這個帖子Click here to see the referred post

我試圖讓訪問哪些是在後指令之後另一個.js文件中定義的函數。不過,我仍然有一個問題,請參見下面我的代碼:

sildemenu.js

$(document).ready(function() { 
    var window.slideMenu=function(){ 
     //do something here 
    }(); 
}); 

control.js

$(document).ready(function() { 
    $('#foo').on('click', function() { 
     window.slideMenu(); 
    }); 
}); 

我得到了錯誤「Object [object Window] has no method'sildeMenu'」。 我在編程方面很新穎。請給我一個憐憫。

+1

的'執行,因爲'VAR window.slideMenu'是一個語法錯誤(和gdoron提到這個問題)sildemenu.js'失敗。放下'var'。我建議閱讀http://www.netmagazine.com/tutorials/javascript-debugging-beginners。 – 2013-02-20 10:11:06

+0

如果你正確地閱讀了鏈接的問題/答案,你會發現在函數定義之後沒有'()','window ....'之前沒有'var'。 – 2013-02-20 10:15:18

回答

1

您嘗試定義一個複雜變量(這是不可能的),而不是爲全局對象賦值(window)。

var window.slideMenu=function(){ 
//^^^ Get rid of this 
    //do something here 
    }(); 
//^^ and remove this 

,擺脫了var 固定代碼:

window.slideMenu=function(){ 
    //do something here 
}; 
+0

我確實應用了你的固定代碼,但是,我仍然得到了同樣的錯誤。看起來'slidemenu.js'很好,但是錯誤來自'control.js'。 – 2013-02-20 10:29:42

0

沒有必要window對象的只寫:

sildemenu.js

$(document).ready(function() { 
    slideMenu=function(){ 
     //Do your stuff here! 
    }; 
}); 

control.js

$(document).ready(function() { 
    $('#foo').on('click', function() { 
     slideMenu(); 
    }); 
}); 
相關問題