2012-06-19 123 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 * /});'? –

相关问题