我可以使用HTML元素的id作为JavaScript中的变量吗?(复制)

时间:2022-08-27 17:42:21

This question already has an answer here:

这个问题已经有了答案:

Accidentally, I noticed I could use the id of an HTML element in JavaScript code. So instead of this:

无意中,我注意到我可以在JavaScript代码中使用HTML元素的id。而不是:

var myCanvas = document.getElementById('myCanvas');
myCanvas.width = '600';
myCanvas.height = '400';

I could simply not even have the first line, because the variable myCanvas apparently already exists!

我甚至连第一行都没有,因为变量myCanvas显然已经存在!

myCanvas.width = '600';
myCanvas.height = '400';

This is nice, but can I rely on it? Is this normal behavior that I can expect in all modern browsers? I don't care about any browsers before IE9.

这很好,但我能相信吗?这种行为在所有现代浏览器中都是正常的吗?我不关心IE9之前的浏览器。

3 个解决方案

#1


3  

In the early days of browser scripting, IE made ID and NAME attribute values into properties of the global object that referenced the related elements. That was widely considered a "bad thing", but was copied by most other browsers in order to be compatible with IE (most sites at the time were written almost exclusively for IE, which had about 95% user share).

在浏览器脚本编写的早期,IE将ID和NAME属性值设置为引用相关元素的全局对象的属性。这被广泛认为是一件“坏事”,但为了与IE兼容,大多数浏览器都复制了它(当时大多数网站几乎都是专为IE编写的,IE拥有95%的用户份额)。

Then came open standards and a concerted effort to support them. Now no one with any sense uses it, though it's still supported by probably all browsers in use.

然后是开放标准和共同努力来支持他们。现在没有任何有意义的人使用它,尽管它可能仍然被所有正在使用的浏览器所支持。

Note that declared global variables of the same name take precedence over a same–named or ID'd element.

注意,声明的同名全局变量优先于同名或ID d元素。

#2


3  

I'm going to refer you to this answer and mention that this is not standard behaviour. This behaviour is supported by all browsers (sans Firefox outside of quircks mode) but I would not recommend its use. This is not supported by firefox<14 in standards mode

我将向你们介绍这个答案并指出这不是标准行为。所有浏览器都支持这种行为(除了quircks模式外,没有Firefox),但我不建议使用它。在标准模式下,firefox<14不支持这一点

#3


2  

You can rely on it. But if the language running the site is ASP.NET, it is safer to use the ClientID. If the ID of the control changes, your code will adapt.

你可以信赖它。但是如果运行站点的语言是ASP的话。NET,使用ClientID更安全。如果控件的ID发生变化,您的代码就会适应。

e.g. For Javascript running over ASP.NET

用于在ASP.NET上运行的Javascript

var myCanvas = document.getElementById("<%=myCanvas.ClientID%>");

#1


3  

In the early days of browser scripting, IE made ID and NAME attribute values into properties of the global object that referenced the related elements. That was widely considered a "bad thing", but was copied by most other browsers in order to be compatible with IE (most sites at the time were written almost exclusively for IE, which had about 95% user share).

在浏览器脚本编写的早期,IE将ID和NAME属性值设置为引用相关元素的全局对象的属性。这被广泛认为是一件“坏事”,但为了与IE兼容,大多数浏览器都复制了它(当时大多数网站几乎都是专为IE编写的,IE拥有95%的用户份额)。

Then came open standards and a concerted effort to support them. Now no one with any sense uses it, though it's still supported by probably all browsers in use.

然后是开放标准和共同努力来支持他们。现在没有任何有意义的人使用它,尽管它可能仍然被所有正在使用的浏览器所支持。

Note that declared global variables of the same name take precedence over a same–named or ID'd element.

注意,声明的同名全局变量优先于同名或ID d元素。

#2


3  

I'm going to refer you to this answer and mention that this is not standard behaviour. This behaviour is supported by all browsers (sans Firefox outside of quircks mode) but I would not recommend its use. This is not supported by firefox<14 in standards mode

我将向你们介绍这个答案并指出这不是标准行为。所有浏览器都支持这种行为(除了quircks模式外,没有Firefox),但我不建议使用它。在标准模式下,firefox<14不支持这一点

#3


2  

You can rely on it. But if the language running the site is ASP.NET, it is safer to use the ClientID. If the ID of the control changes, your code will adapt.

你可以信赖它。但是如果运行站点的语言是ASP的话。NET,使用ClientID更安全。如果控件的ID发生变化,您的代码就会适应。

e.g. For Javascript running over ASP.NET

用于在ASP.NET上运行的Javascript

var myCanvas = document.getElementById("<%=myCanvas.ClientID%>");