简体   繁体   English

onclick或onClick?

[英]onclick or onClick?

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 here 4 times by 3 people, so it can't be a typo and I doubt that it's a coincidence. 我认为在javascript中绑定click事件是通过使用node.onclick完成的,而Chrome / Firefox似乎同意我,但我看到它写了.onClick 这里 4次3人,所以它不能是一个拼写错误我怀疑这是巧合。

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. 我不知道哪一个是最合理的,所以明天我会接受基于民众投票的答案。

Because some browsers (depending on the DOCTYPE) are tolerant of the inline onClick="something();" 因为某些浏览器(取决于DOCTYPE)可以容忍内联onClick="something();" attribute...it seems to have spread a bit, even into JavaScript questions where it doesn't work, since case matters. 属性...它似乎已经传播了一点,甚至在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 ...人们在问题中使用它...好吧,大多数时候他们不会问他们的代码是否有效:)

@Nick Craver pretty much has it nailed down and has my vote; @Nick Craver几乎把它钉在了我的投票上; I just wanted to add my thought. 我只是想加上我的想法。

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. 我认为onClick通常用于对话,因为它更具可读性,并且是我们这些早于所有小写HTML的习惯。 However, in code - both JavaScript and HTML, onclick is correct and the only way it should appear. 但是,在代码中 - 包括JavaScript和HTML, onclick是正确的,它应该出现的唯一方式。 Even if you're using an older HTML doctype, stick to lowercase. 即使您使用较旧的HTML文档类型,也要坚持使用小写。 If you ever update to a more strict doctype, you'll be glad your code doesn't need to be checked for case. 如果您更新到更严格的doctype,您会很高兴您的代码不需要检查案例。

It's just that for most browsers HTML attributes are case insensitive, but JS is case-sensitive. 对于大多数浏览器而言,HTML属性不区分大小写,但JS区分大小写。 onClick will work in html, but if you're defining the handler in JS, you need to use the lowercased onclick. onClick将在html中工作,但如果你在JS中定义处理程序,则需要使用小写的onclick。

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. 我在引用的问题上编辑了我的答案以适应,感谢您指出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM