Html 从一个需要另一个 JavaScript 文件调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17481113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:47:57  来源:igfitidea点击:

Calling a function from one JavaScript file which requires another

javascripthtml

提问by AloNE

I am trying to call a function written in one JavaScript file from another JavaScript file. I have the following code, but it doesn't work:

我正在尝试从另一个 JavaScript 文件调用在一个 JavaScript 文件中编写的函数。我有以下代码,但它不起作用:

My HTML file

我的 HTML 文件

<script type="text/javascript" src="js1.js"></script>
<script type="text/javascript" src="js2.js"></script>
<script language="javascript">
    js1();
</script>

js1.js

js1.js

function js1()
{
    alert("Hello from js1");
    js2();
}

js2.js

js2.js

function js2() 
{
    alert("Hello from js2");
}

What can I do?

我能做什么?

采纳答案by Khanh TO

Try changing the order

尝试更改顺序

<script type="text/javascript" src="js2.js"></script>
<script type="text/javascript" src="js1.js"></script>
<script language="javascript">
   js1();
</script>

Because you call js2();inside js1.js, so the script js2.jsshould be executed before.

因为你调用js2();inside js1.js,所以脚本js2.js应该在之前执行。

In your case, i think it should still work without changing orders like this because you call js2();inside a function. When this script is executed:

在您的情况下,我认为它仍然可以在不更改这样的订单的情况下工作,因为您js2();在函数内部调用。执行此脚本时:

function js1()
{
   alert("Hello from js1");
   js2();
}

Even the js2.jsis not executed yet, but you do notactually call js2();at this time.

即使js2.js尚未执行,但此时您实际上并未调用js2();

Just try it to see if it works.

只需尝试一下,看看它是否有效。

回答by doppelgreener

I'm going to assume that's your entireHTML page.

我将假设这是您的整个HTML 页面。

In order to have those scripts run, you need to have those JavaScript files in the same folder as your webpage, and to actually have a proper HTML page!

为了让这些脚本运行,您需要将这些 JavaScript 文件与您的网页放在同一个文件夹中,并且实际上有一个正确的 HTML 页面!

In your HTML page, you need to include the references to your js1 and js2 files in either the head or body, and include the script you've written in the HTML page itself in the body so that it'll execute when it's loaded:

在您的 HTML 页面中,您需要在头部或正文中包含对 js1 和 js2 文件的引用,并将您在 HTML 页面本身中编写的脚本包含在正文中,以便它在加载时执行:

<!DOCTYPE html>
<!-- ^ Declaring this DOCTYPE means this is a HTML5 page. -->
<html>
    <head>
        <!-- This will load your scripts into the document. -->
        <script src="js1.js"></script>
        <script src="js2.js"></script>
        <!--
            In a HTML5 page, you don't need to include the
            'type="text/javascript"' attribute on script tags.
            They're treated as having that by default, unless you say otherwise.
        -->
    </head>
    <body>
        <!--
            You could also include your scripts here, but I'll
            just leave these commented out since they're already included.
        <script src="js1.js"></script>
        <script src="js2.js"></script>
        -->
        <script>
        js1();
        </script>
        <!--
            You don't need 'language="javascript"' on a script tag.
            Use the type attribute, or nothing in a HTML5 page.
        -->
    </body>
</html>