JavaScript 中的 onclick 事件函数
我在带有按钮的 HTML 页面中有一些 JavaScript 代码。我有一个名为 click()
的函数,用于处理按钮的 onClick
事件。按钮的代码如下:
<input type="button" onClick="click()">button text</input>
问题是,当单击按钮时,没有调用该函数。我在这里做错了什么?
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:
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
两个观察结果:
你应该写
而不是
您应该重命名您的函数。函数
click()
已在按钮上定义(它模拟单击),并且获得比您的方法更高的优先级。请注意,这里有一些建议是完全错误的,您不应该在它们上花费太多时间:
onclick="javascript:myfunc()"
。仅在超链接的href
属性内使用javascript:
前缀:。
onclick="foo()"
和onclick="foo();"
都工作得很好。onclick
、onClick
和ONCLICK
都可以工作。通常的做法是用小写字母编写属性:onclick
。 请注意,JavaScript 本身区分大小写,因此如果您编写document.getElementById("...").onclick = ...
,那么它必须全部小写。Two observations:
You should write
instead of
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.Note that there are a couple of suggestions here that are plain wrong, and you shouldn't spend to much time on them:
onclick="javascript:myfunc()"
. Only use thejavascript:
prefix inside thehref
attribute of a hyperlink:<a href="javascript:myfunc()">
.onclick="foo()"
andonclick="foo();"
both work just fine.onclick
,onClick
andONCLICK
all work. It is common practice to write attributes in lowercase:onclick
. note that javascript itself is case sensitive, so if you writedocument.getElementById("...").onclick = ...
, then it must be all lowercase.click() 是一个保留字并且已经是一个函数,将名称从 click() 更改为 runclick() 它可以正常工作
click() is a reserved word and already a function, change the name from click() to runclick() it works fine
试试这个
Try this
检查您是否正在调用相同的函数。
Check you are calling same function or not.
尝试修复大小写。
onclick
而不是onClick
参考:Mozilla开发者文档
Try fixing the capitalization.
onclick
instead ofonClick
Reference: Mozilla Developer Docs
我建议你这样做:
希望这对您有帮助!
I suggest you do:
<input type="button" value="button text" onclick="click()">
Hope this helps you!
我遇到了同样的问题,onclick 函数调用不起作用。我已将该函数包含在通常的“$(document).ready(function(){});”中block 用于包装 jquery 脚本。评论这个块解决了这个问题。
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.
是的,您应该更改函数的名称。 Javascript有保留方法和onclick = >>>>>单击()<<<<是其中之一,所以只需重命名它,在其末尾添加一个“s”或其他内容即可。
强文本`
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`
今天这也发生在我身上。函数名可能与关键字冲突。我的例子是
scrape()
。我更改了函数名称,一切正常。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.项目不响应事件的一个可能原因是该项目与另一个项目重叠。
在这种情况下,您可能需要设置更高的
z-index
代表您想要点击的项目。One possible cause for an item not responding to an event is when the item is overlapped by another.
In that case, you may have to set a higher
z-index
for the item you wish to click on.