JavaScript和为什么大写字母有时工作,有时不工作 [英] JavaScript and why capital letters sometimes work and sometimes don't

查看:166
本文介绍了JavaScript和为什么大写字母有时工作,有时不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在记事本++中,我正在编写一个JavaScript文件,并且某些内容不起作用:单击按钮时必须显示一个警报,但不起作用。

In Notepad++, I was writing a JavaScript file and something didn't work: an alert had to be shown when a button was clicked, but it wasn't working.

我使用了Notepad ++提供的自动完成插件,它向我介绍了 onClick

I has used the auto-complete plugin provided with Notepad++, which presented me with onClick.

我将首都 C 更改为一个小的 c ,它的工作正常。

When I changed the capital C to a small c, it did work.

首先,在查看自动完成功能时,我注意到使用大写字母的功能很多。

So first of all, when looking at the functions in the auto-completion, I noticed a lot of functions using capitals.

但是当您更改 getElementById getelementbyid ,您也会收到错误,更糟糕的是,我的学校手册将所有内容写入大写字母,但是解决方案都是用小写字母完成的。

But when you change getElementById to getelementbyid, you also get an error, and to make matters worse, my handbook from school writes all the stuff with capital letters but the solutions are all done in small letters.

那么JavaScript是什么,它的选择性是哪些函数可以在其中有大写字母, t?

So what is it with JavaScript and its selective nature towards which functions can have capital letters in them and which can't?

推荐答案

Javascript是总是区分大小写,html不是。

Javascript is ALWAYS case-sensitive, html is not.

这听起来像你在谈论html属性(例如onclick)是或不区分大小写。答案是属性不区分大小写,但是我们通过DOM访问它们的方式是。
所以你可以这样做:

It sounds as thought you are talking about whether html attributes (e.g. onclick) are or are not case-sensitive. The answer is that the attributes are not case sensitive, but the way that we access them through the DOM is. So, you can do this:

<div id='divYo' onClick="alert('yo!');">Say Yo</div> // Upper-case 'C'

或:

<div id='divYo' onclick="alert('yo!');">Say Yo</div> // Lower-case 'C'

但是通过DOM,您必须使用正确的大小写。所以这样做:

but through the DOM you must use the correct case. So this works:

getElementById('divYo').onclick = function() { alert('yo!'); }; // Lower-case 'C'

但是你不能这样做:

getElementById('divYo').onClick = function() { alert('yo!'); }; // Upper-case 'C'

编辑:CMS表明大多数DOM方法和属性在 camelCase 。想到的一个例外是事件处理程序属性,这些属性通常被接受为错误的方法无论如何,附加事件。喜欢使用 addEventListener ,如:

CMS makes a great point that most DOM methods and properties are in camelCase. The one exception that comes to mind are event handler properties and these are generally accepted to be the wrong way to attach to events anyway. Prefer using addEventListener as in:

document.getElementById('divYo').addEventListener('click', modifyText, false);

这篇关于JavaScript和为什么大写字母有时工作,有时不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆