Javascript onclick 还是 onClick?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4380719/
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
onclick or onClick?
提问by Gabi Purcaru
I thought that binding the click event in javascript is done by using node.onclick
, and Chrome/Firefox seem to agree with me, but I saw it written .onClick
here4 times by 3 people, so it can't be a typo and I doubt that it's a coincidence.
我认为在 javascript 中绑定 click 事件是通过 using 来完成的node.onclick
,Chrome/Firefox 似乎也同意我的看法,但我看到3 个人.onClick
在这里写了4 次,所以它不可能是一个错字,我怀疑它是一个巧合。
So, why are people writing onClick
when it does not work?
那么,为什么人们onClick
在它不起作用的时候写作呢?
UPDATE: There are two good explanations; I don't know which one of them is the most plausible, so I will accept the answer based on popular vote, tomorrow.
更新:有两个很好的解释;我不知道其中哪一个最有道理,所以明天我将接受基于普选的答案。
采纳答案by Nick Craver
Because some browsers (depending on the DOCTYPE) are tolerant of the inline onClick="something();"
attribute...it seems to have spread a bit, even into JavaScript questions where it doesn't work, since case matters.
因为某些浏览器(取决于 DOCTYPE)可以容忍 inlineonClick="something();"
属性......它似乎已经传播了一点,甚至在它不起作用的 JavaScript 问题中也是如此,因为大小写很重要。
Also, specifically to stackoverflow...people using it in questions...well, most of the time they wouldn't be asking a question if their code worked :)
此外,特别是对于 stackoverflow ......人们在问题中使用它......好吧,如果他们的代码有效,大多数时候他们不会问问题:)
回答by Surreal Dreams
@Nick Craver pretty much has it nailed down and has my vote; I just wanted to add my thought.
@Nick Craver 几乎已经确定并获得了我的投票;我只是想补充一下我的想法。
I think it's onClick
is often used in conversation because it's a bit more readable, and as an old habit from those of us who predate all lowercase HTML. However, in code - both JavaScript and HTML, onclick
is correct and the only way it should appear. Even if you're using an older HTML doctype, stick to lowercase. If you ever update to a more strict doctype, you'll be glad your code doesn't need to be checked for case.
我认为它onClick
经常在对话中使用,因为它更具可读性,并且是我们这些早于所有小写 HTML 的人的旧习惯。然而,在代码中——JavaScript 和 HTMLonclick
都是正确的,也是它应该出现的唯一方式。即使您使用的是较旧的 HTML 文档类型,也要坚持使用小写字母。如果您更新到更严格的文档类型,您会很高兴您的代码不需要检查大小写。
回答by Gopherkhan
It's just that for most browsers HTML attributes are case insensitive, but JS is case-sensitive. onClick will work in html, but if you're defining the handler in JS, you need to use the lowercased onclick.
只是对于大多数浏览器来说,HTML 属性是不区分大小写的,而 JS 是区分大小写的。onClick 将在 html 中工作,但如果您在 JS 中定义处理程序,则需要使用小写的 onclick。
回答by Orbling
In that specific question, I used "onClick" as the original question had it in that vein and I try to change as little of the OPs code as possible to make a solution, so that they can see their mistake easily.
在那个特定问题中,我使用了“onClick”作为原始问题的意思,并且我尝试尽可能少地更改 OP 代码以解决问题,以便他们可以轻松地看到他们的错误。
The camel case is invalid technically, though I like camel case in general. Frankly, it always annoys me when I see that method, as I think "Where's jQuery!".
骆驼套在技术上是无效的,虽然我一般喜欢骆驼套。坦率地说,当我看到那个方法时总是让我烦恼,因为我想“jQuery 在哪里!”。
It is heavily prevalent in the world, I see it all the time in source.
它在世界上非常流行,我一直在源代码中看到它。
I edited my answer on the referenced question to fit, thanks for pointing it out.
我编辑了我对引用问题的回答以适应,感谢您指出。