簡體   English   中英

無法從我的HTML頁面調用javascript文件

[英]Can't call javascript file from my html page

我是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/)會發現代碼中的許多問題。 只需復制粘貼並更正錯誤即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM