简体   繁体   English

在IE11和IE8中引用DOM元素

[英]Referencing DOM elements in IE11 vs IE8

I have a website that contains the following HTML/javascript: 我有一个包含以下HTML / javascript的网站:

<input type="text" id="txtPhaseID" onkeypress="return onlyNumbers();" />
<a id="btnLookup" onclick="GetEventLookupData(txtPhaseID.value);this.blur();return false;" href="javascript:void(0);" class="btn"><span>Lookup</span></a>

The reference to txtPhaseID.value in the onclick event works in IE8 but fails in IE11. 在onclick事件中对txtPhaseID.value的引用在IE8中有效,但在IE11中失败。 I'd expect it to fail in both since you should have to use document.getElementById. 我希望这两种方法都会失败,因为您必须使用document.getElementById。

Why does this work in IE8? 为什么在IE8中可以使用?

Technically when you add an id to an element you could access it directly from the DOM with id.property - but it's always been considered very bad practice to a point that no one was ever taught that way. 从技术上讲,当您向一个元素添加一个id时,您可以使用id.property从DOM直接访问它-但一直被认为是一种非常糟糕的做法,以至于从未有人教过这种方式。 I can only imagine it was finally removed in Internet Explorer 11. I have no idea what this is called and I don't think it's ever been given a specific name, which means Googling will return pretty limited results. 我只能想象它最终在Internet Explorer 11中被删除了。我不知道这叫什么名字,也不认为它曾被赋予一个特定的名称,这意味着Googling只会返回有限的结果。 Unfortunately, if they did remove it there probably isn't anything saying so. 不幸的是,如果他们确实删除了它,那么可能什么也没说。

Here are some other resources from stackoverflow for more information: 这是来自stackoverflow的其他一些资源,以获取更多信息:

Do DOM tree elements with ids become global variables? 具有ID的DOM树元素是否会成为全局变量?

Why don't we just use element IDs as identifiers in JavaScript? 为什么我们不只使用元素ID作为JavaScript中的标识符?

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

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