onclick 事件在 JavaScript 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5453937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:15:22  来源:igfitidea点击:

onclick event not working in JavaScript

javascripthtmljavascript-eventsevent-driven

提问by Nate Koppenhaver

I have some JavaScript code in an HTML page with a button. I have a function called 'click()' that handles the onClick event of the button. The code for the button is as follows:

我在带有按钮的 HTML 页面中有一些 JavaScript 代码。我有一个名为“click()”的函数,用于处理按钮的 onClick 事件。按钮的代码如下:

<input type="button" onClick="click()">button text</input>  

The problem is that when the button is clicked, the function is not called. What am I doing wrong here?
Thanks

问题是当按钮被点击时,函数没有被调用。我在这里做错了什么?
谢谢

回答by Elian Ebbing

Two observations:

两个观察:

  1. You should write

    <input type="button" value="button text" />
    

    instead of

    <input type="button">button text</input>
    
  2. You should rename your function. The function click()is already defined on a button (it simulates a click), and gets a higher priority then your method.

  1. 你应该写

    <input type="button" value="button text" />
    

    代替

    <input type="button">button text</input>
    
  2. 您应该重命名您的函数。该函数click()已在按钮上定义(它模拟单击),并且获得比您的方法更高的优先级。

Note that there are a couple of suggestions here that are plain wrong, and you shouldn't spend to much time on them:

请注意,这里有一些建议是完全错误的,您不应该在它们上花费太多时间:

  • Do not use onclick="javascript:myfunc()". Only use the javascript:prefix inside the hrefattribute of a hyperlink: <a href="javascript:myfunc()">.
  • You don't have to end with a semicolon. onclick="foo()"and onclick="foo();"both work just fine.
  • Event attributes in HTML are not case sensitive, so onclick, onClickand ONCLICKall work. It is common practice to write attributes in lowercase: onclick. note that javascript itself is case sensitive, so if you write document.getElementById("...").onclick = ..., then it mustbe all lowercase.
  • 不要使用onclick="javascript:myfunc()". 仅在超链接javascript:href属性中使用前缀:<a href="javascript:myfunc()">
  • 您不必以分号结尾。onclick="foo()"并且onclick="foo();"都工作得很好。
  • HTML 中的事件属性不区分大小写,因此onclick,onClickONCLICK所有工作。以小写形式编写属性是常见的做法:onclick. 请注意,javascript 本身是区分大小写的,因此如果您编写document.getElementById("...").onclick = ...,那么它必须全部为小写

回答by Jimmy Rousseau

click() is a reserved word and already a function, change the name from click() to runclick() it works fine

click() 是一个保留字并且已经是一个函数,将名称从 click() 更改为 runclick() 就可以了

回答by Nathanphan

Try this

尝试这个

<input type="button" onClick="return click();">button text</input>  

回答by user3669026

Check you are calling same function or not.

检查您是否正在调用相同的函数。

<script>function greeting(){document.write("hi");}</script>

<input type="button" value="Click Here" onclick="greeting();"/>

回答by Wylie

Try fixing the capitalization. onclickinstead of onClick

尝试修复大写。onclick代替onClick

Reference: Mozilla Developer Docs

参考:Mozilla 开发者文档

回答by Kyle Gagnon

I suggest you do: <input type="button" value="button text" onclick="click()">Hope this helps you!

我建议你这样做: <input type="button" value="button text" onclick="click()">希望这对你有帮助!

回答by Tony

<script>
//$(document).ready(function () {
function showcontent() {
        document.getElementById("demo22").innerHTML = "Hello World";
}
//});// end of ready function
</script>

I had the same problem where onclick function calls would not work. I had included the function inside the usual "$(document).ready(function(){});" block used to wrap jquery scripts. Commenting this block out solved the problem.

我遇到了同样的问题,onclick 函数调用不起作用。我在通常的“$(document).ready(function(){});”中包含了这个函数 用于包装 jquery 脚本的块。注释掉这个块解决了这个问题。

回答by Jerry Noel

Yes you should change the name of your function. Javascript has reserved methods and onclick = >>>> click() <<<< is one of them so just rename it, add an 's' to the end of it or something. strong text`

是的,您应该更改函数的名称。Javascript 有保留的方法,而 onclick = >>>> click() <<<< 是其中之一,所以只需重命名它,在它的末尾添加一个“s”或其他东西。 强文本`

回答by lover summer

Today this also happened to me. The function name maybe conflicts with keywords. My case is scrape(). I change the function name, everything works fine.

今天这也发生在我身上。函数名可能与关键字冲突。我的情况是scrape()。我更改了函数名称,一切正常。