簡體   English   中英

未捕獲的 ReferenceError:在 HTMLButtonElement.onclick 處未定義回顯(index.html:23)

[英]Uncaught ReferenceError: echo is not defined at HTMLButtonElement.onclick (index.html:23)

我有一個非常基本的 javascript 調用一個 function 的問題,這只是一個測試警報,以檢查我到目前為止的代碼是否正常工作。 我在 Chrome 控制台中收到“Uncaught ReferenceError: echo is not defined at HTMLButtonElement.onclick (index.html:23)”。 這是我的索引。html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple Calculator</title>
</head>
<body>
<header>

</header>
<nav></nav>
<main>


 
 <table style="width:100%">
  
 <tr>  <th>Fancy Calculator</hd></tr>
 <td>
 <form>
 <label for="number">Please enter a number between 0 and 50.</label><br>
 <input type="text" id="number" name="number"><br>
 <button type="button" onclick="echo(number.value)">Calculate!</button><br>

  
 <input type="text" id="factorial" name="factorial" disabled>!<br>
 <input type="text" id="squared" name="squared" disabled>&sup2;<br>
 <input type="text" id="cubed" name="cubed" disabled>&#xB3;<br>    
 </form> 
 </td>
 </tr>
  
</table> 
  
  
</main>
<footer>
<!--- Script 2.1 - template.html -->
<script src="js/calc.js"> 
</footer>
</body>
</html>

這是我的 calc.js 文件:

/* calc.js */

console.log("Hello");

function echo( number ) {
    alert("You entered", + number + "!");
}

任何幫助將非常感激。

您在 function 中添加了一個不必要的逗號。

嘗試這個:

function echo(number) {
    alert("You entered: " + number + "!");
}

而且,正如 Quentin 所提到的,確保腳本中的 URL 是正確的。

另外,我認為<script>元素需要使用</script>來關閉,所以將其添加到腳本的末尾以查看它是否有所不同。

<script src="js/calc.js"></script>

暫無
暫無

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

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