简体   繁体   English

未捕获的 ReferenceError:在 HTMLButtonElement.onclick 处未定义回显(index.html:23)

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

I am having a problem with a quite basic bit of javascript to call a function which is just a test alert to check the code I have so far is working.我有一个非常基本的 javascript 调用一个 function 的问题,这只是一个测试警报,以检查我到目前为止的代码是否正常工作。 I am getting "Uncaught ReferenceError: echo is not defined at HTMLButtonElement.onclick (index.html:23)" in the Chrome console.我在 Chrome 控制台中收到“Uncaught ReferenceError: echo is not defined at HTMLButtonElement.onclick (index.html:23)”。 Here is my index.html这是我的索引。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>

Here is my calc.js file:这是我的 calc.js 文件:

/* calc.js */

console.log("Hello");

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

Any help would be much appreciated.任何帮助将非常感激。

You added an unnecessary comma to your function.您在 function 中添加了一个不必要的逗号。

Try this:尝试这个:

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

And, as Quentin mentioned, make sure the URL to your script is correct.而且,正如 Quentin 所提到的,确保脚本中的 URL 是正确的。

Also, I think <script> elements need to be closed using </script> , so add that on the end of your script to see if it makes a difference.另外,我认为<script>元素需要使用</script>来关闭,所以将其添加到脚本的末尾以查看它是否有所不同。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 ReferenceError:测试未在 HTMLButtonElement.onclick ((index):1) 中定义 - Uncaught ReferenceError: test is not defined at HTMLButtonElement.onclick ((index):1) 未捕获的 ReferenceError:“a”未在 HTMLButtonElement.onclick 中定义(gallery.html:1) - Uncaught ReferenceError: 'a' is not defined at HTMLButtonElement.onclick (gallery.html:1) 未捕获的ReferenceError :(函数)未在HTMLButtonElement.onclick中定义 - Uncaught ReferenceError: (function) not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick中定义查找 - Uncaught ReferenceError: lookup is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:showCurrentPage 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: showCurrentPage is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:在 HTMLButtonElement.onclick 中未定义 postAcmgForm - Uncaught ReferenceError: postAcmgForm is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:HTMLButtonElement.onclick中未定义submitToAPI - Uncaught ReferenceError: submitToAPI is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:id 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: id is not defined at HTMLButtonElement.onclick Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick - Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义函数 - Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM