如何在.js文件中调用.html中的javascript函数

swo*_*osh 2 html javascript jquery function

是否可以在.js文件中从.html文件调用javascript函数?例如,我在我的foo.js中有这个:

$(document).ready(function() {
    fn(); 
})
Run Code Online (Sandbox Code Playgroud)

我想调用我的index.html中的fn():

<script type="text/javascript" src="js/foo.js"></script>
<script type="text/javascript">
function fn() {
     .....
}
</script>
Run Code Online (Sandbox Code Playgroud)

当我这样做时,似乎没有调用fn().

ahr*_*ren 8

您的脚本标记嵌套不正确.

<script type="text/javascript">
    function fn() {
         .....
    }
</script>
<script type="text/javascript" src="js/foo.js"></script>
Run Code Online (Sandbox Code Playgroud)

并实际回答你的问题:是的,这是可能的.