2012-06-19 57 views
1

問題從另一個腳本調用函數?

<script type="text/javascript" src="http://localhost/ci/js/global_functions.js"></script> 
<script type="text/javascript" src="http://localhost/ci/js/global.js"></script> 

爲什麼不能global.js發現我在global_functions.js創建的函數;現在的我只能夠訪問該功能使用window.helper = { func: function() {} }

代碼

$(document).ready(function() { 
    function id(input_id) { 
     return document.getElementById(input_id); //global_functions.js 
    } 
} 

$(document).ready(function() { 
    $(id('home_login')).css('display', 'none'); //global.js 
} 
+1

可能是你的功能沒有在全球範圍內 –

回答

6

最可能的原因是,你定義在非全局範圍的功能。雖然你沒有向我們展示代碼,但很難肯定地說。


更新,現在的代碼已添加:

這就是正在發生的事情。

function() {     // This is a function 
    function id(input_id) { // So this function is scoped to it 
     return document.getElementById(input_id); 
    } 
} 
+0

定義是什麼,我想,但都處於'的document.ready()'函數,我的印象是,他們都在同一發射時間。 – Phil

+0

他們依次開火,但問題是* scope * not * timing *之一。如果函數是在傳遞給'ready()'的匿名函數中定義的,那麼該函數僅在匿名函數的範圍內可用。 – Quentin

+0

你的意思是你在**文檔裏聲明瞭函數**準備好了嗎?如$(document).ready(function(){/ * declare function x here * /});'?或者你的意思是你調用文檔內的函數準備'$(document).ready(function(){/ * call function x here * /});'? –