简体   繁体   English

html元素id为javascript变量

[英]html element id as javascript variable

Consider the following code: 请考虑以下代码:

<html>
<head></head>
<body>
<div id='test' class='blah'>
  <a href='http://somesite.com/' id='someLink'>click!</a>
</div>
</body>
</html>

So I just recently discovered that this creates a javascript object called someLink and I can for instance get the value of the href attribute with someLink.href . 所以我最近发现这创建了一个名为someLink的javascript对象,我可以通过someLink.href获取href属性的值。 I tested this in the latest Chrome, FF and IE and it works. 我在最新的Chrome,FF和IE中对此进行了测试,它确实有效。

First off, how long has this "feature" been around? 首先,这个“功能”到底有多长? I imagine probably a while, because I have known for years that IDs for html elements on a page must be unique, and if you do have more than one element sharing the same ID, the last one overwrites the previous one(s), and using for instance getElementById() will return the last one. 我想可能有一段时间了,因为我已经知道多年来页面上html元素的ID必须是唯一的,如果你有多个元素共享相同的ID,最后一个元素会覆盖前一个元素,并且使用例如getElementById()将返回最后一个。 But I never really understood why , but now, looking at it as a "this is creating an object" perspective, it makes sense. 但我从来没有真正理解为什么 ,但现在,把它视为“这是创造一个对象”的观点,这是有道理的。 So, as far as being able to directly access it with the id-name-as-javascript object...how long has that been around? 所以,只要能够使用id-name-as-javascript对象直接访问它...它有多久了? IE6 era? IE6时代? Earlier? 早些时候?

2nd...I guess this is more of a discussion point than question, but... IMO this doesn't seem like a very good "feature" to have... Isn't the whole point of having a DOM and wrapper functions like getElementById() , to give some organization and more importantly, cut down on namespace issues? 第二......我想这更像是一个讨论点而不是问题,但是... IMO这似乎不是一个非常好的“功能”......不是有一个DOM和包装器的重点像getElementById()这样的函数,给一些组织,更重要的是,减少名称空间问题? I don't feel I should have to be worried about random html elements on a page overwriting my javascript variables (something that has recently happened, which is why I discovered this "feature"). 我不觉得我应该担心页面上的随机html元素会覆盖我的javascript变量(最近发生的事情,这就是我发现这个“功能”的原因)。 Does anybody know why this is as it is, what's the logic behind it? 有谁知道为什么会这样,它背后的逻辑是什么?

First off, how long has this "feature" been around? 首先,这个“功能”到底有多长?

It is a Microsoft-ism that cropped up around IE 4 if I remember correctly. 如果我没记错的话,它是一个微软主义的IE 4。

Some other browsers have added support for it in an effort to be compatible with badly written code that depends on it. 其他一些浏览器已经添加了对它的支持,以便与依赖于它的编写错误的代码兼容。 Some may only support it in quirks mode . 有些人可能只在怪异模式下支持它。

this doesn't seem like a very good "feature" to have 这似乎不是一个非常好的“功能”

Correct. 正确。 Don't use it. 不要使用它。 :) :)

Yes, it's been around for a very long time, which is likely the only reason it exists. 是的,它已经存在了很长时间,这可能是它存在的唯一原因。 Removing such a "feature" will break compatibility with existing web sites, which browser vendors are very reluctant to do. 删除这样的“功能”将破坏与现有网站的兼容性,浏览器供应商非常不愿意这样做。

You're correct in saying that it's not a good idea. 你说这不是一个好主意是对的。 It's a profoundly bad idea and can lead to naming conflicts as well as unnecessary pollution of the global namespace. 这是一个非常糟糕的主意,可能导致命名冲突以及全局命名空间的不必要污染。

It's an old IE feature, not recommended to use. 这是一个旧的IE功能,不推荐使用。 Several browsers implement it, often as nonenumerable properties of the window object. 一些浏览器实现它,通常作为窗口对象的不可数属性。 FF, for example, does it only in quirks mode. 例如,FF仅在怪异模式下执行。

For further reading see duplicate Is there a spec that the id of elements should be made global variable? 有关进一步阅读,请参阅重复是否有规范要求元素的id应为全局变量? ... ...

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

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