Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

javascript - onclick事件无法在JavaScript中运行(onclick event not working in JavaScript)

I have some JavaScript code in an HTML page with a button.(我在带有按钮的HTML页面中有一些JavaScript代码。)

I have a function called 'click()' that handles the onClick event of the button.(我有一个名为'click()'的函数来处理按钮的onClick事件。) 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?(我在这做错了什么?)
Thanks(谢谢)   ask by Nate Koppenhaver translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()" .(不要使用onclick="javascript:myfunc()" 。) Only use the javascript: prefix inside the href attribute of a hyperlink: <a href="javascript:myfunc()"> .(只在超链接的href属性中使用javascript:前缀: <a href="javascript:myfunc()"> 。) You don't have to end with a semicolon.(你不必以分号结尾。) onclick="foo()" and onclick="foo();"(onclick="foo()"onclick="foo();") both work just fine.(两者都很好。) Event attributes in HTML are not case sensitive, so onclick , onClick and ONCLICK all work.(HTML中的事件属性不区分大小写,因此onclickonClickONCLICK都可以正常工作。) It is common practice to write attributes in lowercase: onclick .(通常的做法是以小写形式写入属性: onclick 。) note that javascript itself is case sensitive, so if you write document.getElementById("...").onclick = ... , then it must be all lowercase .(请注意,javascript本身区分大小写,因此如果您编写document.getElementById("...").onclick = ... ,则它必须全部为小写 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...