外部 javascript 文件中的函数。我究竟做错了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5302256/
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-10-25 16:42:52  来源:igfitidea点击:

Functions in an external javascript file. What am i doing wrong?

javascriptfunction

提问by hhh3112

I'm new to javascript, and I'm trying to run the following code:

我是 javascript 新手,我正在尝试运行以下代码:

<html>
<head>
    <script type="text/javascript" src="script.js">
    </script>
</head>
<body>
    <input type="button" onclick="popup()" value="Click Me!" />
</body>
</html>

script.js:

脚本.js:

function popup() {
    alert("Hello World")
}

script.js is in the same directory as the html file, but when I push the button it doesn't execute the code.

script.js 与 html 文件在同一目录中,但是当我按下按钮时它不执行代码。

回答by smartcaveman

both the alert()in your function and your call to the function needs to be closed with a ;. So,

无论是alert()在功能和您的调用函数需要一个被关闭;。所以,

<html>
<head>
<script type="text/javascript" src="script.js" />
</head>
<body>
    <input type="button" onclick="popup();" value="Click Me!" />
</body>
</html>

script.js:

脚本.js:

function popup() {
    alert("Hello World");
}

回答by Vamsi Krishna B

<html>
<head>
    <script type="text/javascript" src="button.js">
    </script>
</head>
<body>
    <input type="button" onclick="popup();" value="Click Me!" />
</body>
</html>

you just missed a semicolon.

你只是错过了一个分号

回答by GG.

<html>
<head>
    <script type="text/javascript">
        function popup() { alert('lol'); }
    </script>
</head>
<body>
    <input type="button" onclick="popup();" value="Click Me!" />
</body>
</html>

Like this that's works, check your path is correct.

像这样有效,请检查您的路径是否正确。

回答by The_asMan

semicolons are only needed if you want to combine code on one line

仅当您想在一行上组合代码时才需要分号

alert("Hello World");alert("Hello World");

I copied and pasted your exact code and it worked fine for me. The only explanations are you have an error in your script.js file with another function in it.
Or you have a funky character in the text file that the browser doesn't know what to do with.
Delete all spaces and tabs and try it again.

我复制并粘贴了您的确切代码,对我来说效果很好。唯一的解释是您的 script.js 文件中有一个错误,其中包含另一个函数。
或者您在文本文件中有一个浏览器不知道如何处理的时髦字符。
删除所有空格和制表符,然后重试。

回答by Pablo Grisafi

Your exact code works on my machine, so I think you may have a wrong file name. If you are using Windows, remember Windows hides the extension of files, so you may end up with a file named script.js.txt instead of script.js.
Check the exact file name.
Also, try ggregtheitroade's code to see if you have a pop-up blocker active or something like that.

您的确切代码适用于我的机器,所以我认为您的文件名可能有误。如果您使用的是 Windows,请记住 Windows 隐藏了文件的扩展名,因此您最终可能会得到一个名为 script.js.txt 的文件而不是 script.js。
检查确切的文件名。
此外,尝试使用 ggregtheitroade 的代码,看看您是否有一个弹出窗口阻止程序或类似的东西。