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
问题是单击按钮时,不会调用该函数。我在这做错了什么?谢谢
10 个解决方案
#1
84
Two observations:
两点意见:
-
You should write
你应该写
<input type="button" value="button text" />
instead of
代替
<input type="button">button text</input>
-
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.你应该重命名你的功能。函数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 thejavascript:
prefix inside thehref
attribute of a hyperlink:<a href="javascript:myfunc()">
. - 不要使用onclick =“javascript:myfunc()”。仅在超链接的href属性中使用javascript:前缀:。
- You don't have to end with a semicolon.
onclick="foo()"
andonclick="foo();"
both work just fine. - 您不必以分号结尾。 onclick =“foo()”和onclick =“foo();”两者都很好。
- Event attributes in HTML are not case sensitive, so
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. - HTML中的事件属性不区分大小写,因此onclick,onClick和ONCLICK都可以正常工作。通常的做法是以小写形式写入属性:onclick。请注意,javascript本身区分大小写,因此如果您编写document.getElementById(“...”)。onclick = ...,则它必须全部小写。
#2
9
click() is a reserved word and already a function, change the name from click() to runclick() it works fine
click()是一个保留字并且已经是一个函数,将名称从click()更改为runclick()它可以正常工作
#3
3
Try this
尝试这个
<input type="button" onClick="return click();">button text</input>
#4
1
Check you are calling same function or not.
检查您是否正在调用相同的功能。
<script>function greeting(){document.write("hi");</script>
<input type="button" value="Click Here" onclick="greeting();"/>
#5
0
Try fixing the capitalization. onclick
instead of onClick
尝试修复大小写。 onclick而不是onClick
Reference: Mozilla Developer Docs
参考:Mozilla开发人员文档
#6
0
I suggest you do: <input type="button" value="button text" onclick="click()">
Hope this helps you!
我建议你这样做:希望这对你有帮助!
#7
0
<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脚本的块。评论这个区块解决了这个问题。
#8
0
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”或其他内容。强文.`< p>
#9
0
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.
今天这也发生在我身上。函数名称可能与关键字冲突。我的情况是刮()。我改变了功能名称,一切正常。
#10
-1
Group member just had an issue where the id of the button was the same as the function name, check your button's id, or check other components and make sure the names do not conflict.
组成员遇到的问题是按钮的id与函数名称相同,检查按钮的id,或检查其他组件并确保名称不冲突。
#1
84
Two observations:
两点意见:
-
You should write
你应该写
<input type="button" value="button text" />
instead of
代替
<input type="button">button text</input>
-
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.你应该重命名你的功能。函数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 thejavascript:
prefix inside thehref
attribute of a hyperlink:<a href="javascript:myfunc()">
. - 不要使用onclick =“javascript:myfunc()”。仅在超链接的href属性中使用javascript:前缀:。
- You don't have to end with a semicolon.
onclick="foo()"
andonclick="foo();"
both work just fine. - 您不必以分号结尾。 onclick =“foo()”和onclick =“foo();”两者都很好。
- Event attributes in HTML are not case sensitive, so
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. - HTML中的事件属性不区分大小写,因此onclick,onClick和ONCLICK都可以正常工作。通常的做法是以小写形式写入属性:onclick。请注意,javascript本身区分大小写,因此如果您编写document.getElementById(“...”)。onclick = ...,则它必须全部小写。
#2
9
click() is a reserved word and already a function, change the name from click() to runclick() it works fine
click()是一个保留字并且已经是一个函数,将名称从click()更改为runclick()它可以正常工作
#3
3
Try this
尝试这个
<input type="button" onClick="return click();">button text</input>
#4
1
Check you are calling same function or not.
检查您是否正在调用相同的功能。
<script>function greeting(){document.write("hi");</script>
<input type="button" value="Click Here" onclick="greeting();"/>
#5
0
Try fixing the capitalization. onclick
instead of onClick
尝试修复大小写。 onclick而不是onClick
Reference: Mozilla Developer Docs
参考:Mozilla开发人员文档
#6
0
I suggest you do: <input type="button" value="button text" onclick="click()">
Hope this helps you!
我建议你这样做:希望这对你有帮助!
#7
0
<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脚本的块。评论这个区块解决了这个问题。
#8
0
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”或其他内容。强文.`< p>
#9
0
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.
今天这也发生在我身上。函数名称可能与关键字冲突。我的情况是刮()。我改变了功能名称,一切正常。
#10
-1
Group member just had an issue where the id of the button was the same as the function name, check your button's id, or check other components and make sure the names do not conflict.
组成员遇到的问题是按钮的id与函数名称相同,检查按钮的id,或检查其他组件并确保名称不冲突。