不能从我的html页面调用javascript文件

Can't call javascript file from my html page

本文关键字:调用 javascript 文件 我的 html 不能      更新时间:2023-09-26

我是JavaScript新手,只是想把我的JavaScript代码放在另一个文件中。

这是我的html页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>my badass page</title>
    <script type="text/javascript" scr = "testing.js"></script>//this contains the function I want to call
</head>
<body id="body">
    <button type="button", onclick="showDate()">show the date</button>
</body>
</html>

这是testing.js文件:

function showDate() {
    alert ("this works")
}

我认为我只是犯了一个初学者的错误,因为它看起来很常见,但我不知道。

你在标签上拼错了'src'属性

你拼的是scr,应该是src

:

<script type="text/javascript" scr = "testing.js"></script>

应该是这样的:

<script type="text/javascript" src = "testing.js"></script>

将按钮更改为

<button type="button" onclick="showDate()">show the date</button>

并将SCR改为src

编辑:源代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>my badass page</title>
    <script type="text/javascript" src="testing.js">
    </script>
</head>
<body id="body">
    <button type="button" onclick="showDate();">show the date</button>
</body>
</html>

快速使用html验证器(如http://validator.w3.org/nu/)将发现代码中的许多问题。只需复制粘贴并改正错误。