简体   繁体   English

javascript未捕获引用错误onclick

[英]javascript uncaught reference error onclick

Trying to call a javascript function with a button, and the button is not doing anything on click. 尝试使用按钮调用javascript函数,按钮在点击时没有做任何事情。 The debugger returns "uncaught reference error: printNumber is not defined. 调试器返回“未捕获的引用错误:未定义printNumber。

printNumber is the function I want to call. printNumber是我想要调用的函数。

I did a search and most people with this problem did not use the proper syntax for declaring their function, defined their function within another function (function out of scope), or put their functions after they are called. 我做了一个搜索,大多数有这个问题的人都没有使用正确的语法来声明它们的功能,在另一个函数中定义它们的函数(函数超出范围),或者在它们被调用之后放置它们的函数。 It turns out nothing in my head is running. 事实证明我头脑中没有任何东西在运行。 I tried to put a prompt in it for example, and the prompt did not show up. 我试图在其中添加一个提示,但提示没有显示出来。

Here is my script: 这是我的脚本:

    <head>
    <script type="javascript">
    var x=0;
    function addNumber(x){
        x = x + 1;
        return x;   
    }

    function printNumber(number){
        document.getElementById("number").innerHTML=addNumber(number);
    }

    </script>
    </head>

Here is my HTML: 这是我的HTML:

    <body>
<p><center><b>The number of times the button has been clicked is ... <b><p>
<br>
<div id="number"></div>
<br>
<form>
    <input type="button" id="StartButton" value="Click to add 1 to counter" onClick="printNumber(x)">
</form>
</center>

Can anybody point out to me where the problem is coming from? 任何人都可以向我指出问题的来源吗? Thank you, this is for a programming class, and I hope that others who might also encounter this will see this. 谢谢,这是一个编程类,我希望其他可能遇到这个的人会看到这个。

Use <script type="text/javascript"> and note that you can't increment x in the way you wanted. 使用<script type="text/javascript">并注意您不能以您想要的方式递增x You are incrementing a local variable, not the global one. 您正在递增局部变量,而不是全局变量。

  <html> <head> <script type="text/javascript"> var x=0; function addNumber(){ x = x + 1; return x; } function printNumber(number){ document.getElementById("number").innerHTML=addNumber(); } </script> </head> <body> <p><center><b>The number of times the button has been clicked is ... <b><p> <br> <div id="number"></div> <br> <form> <input type="button" id="StartButton" value="Click to add 1 to counter" onClick="printNumber()"> </form> </center> </body> </html> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM