JavaScript 窗口对象元素属性

发布于 2024-09-01 20:50:23 字数 820 浏览 6 评论 0原文

一位同事向我展示了以下代码并问我为什么它有效。

<span id="myspan">Do you like my hat?</span>
<script type="text/javascript">
var spanElement = document.getElementById("myspan");
alert("Here I am! " + spanElement.innerHTML + "\n" + myspan.innerHTML);
</script>

我解释过,当浏览器解析文档时,会使用元素 id 的名称将一个属性附加到 window 对象,然后该文档包含对相应 dom 节点的引用。就好像在渲染页面时在幕后调用 window.myspan = document.getElementById("myspan") 一样。

在接下来的讨论中,我们提出了一些问题:

  • 窗口对象和大部分 DOM 不是官方 JavaScript/ECMA 标准的一部分,但是上述行为是否记录在任何其他官方文献中,也许与浏览器相关?

  • 上面的代码在浏览器(至少是主要的竞争者)中有效,因为有一个 window 对象,但在像 rhino 这样的东西中失败了。编写依赖于此的代码是否被认为是不好的做法,因为它对执行环境做出了太多假设?

  • 是否有任何浏览器会导致上述操作失败,或者这是否被视为全面的标准行为?

这里有人知道这些问题的答案并愿意启发我吗?我尝试了快速的互联网搜索,但我承认我不确定如何正确地表达查询。欢迎提供参考资料和文档。

A coworker showed me the following code and asked me why it worked.

<span id="myspan">Do you like my hat?</span>
<script type="text/javascript">
var spanElement = document.getElementById("myspan");
alert("Here I am! " + spanElement.innerHTML + "\n" + myspan.innerHTML);
</script>

I explained that a property is attached to the window object with the name of the element's id when the browser parses the document which then contains a reference to the appropriate dom node. It's sort of as if window.myspan = document.getElementById("myspan") is called behind the scenes as the page is being rendered.

The ensuing discussion we had raised a few of questions:

  • The window object and most of the DOM are not part of the official JavaScript/ECMA standards, but is the above behavior documented in any other official literature, perhaps browser-related?

  • The above works in a browser (at least the main contenders) because there is a window object, but fails in something like rhino. Is writing code that relys on this considered bad practice because it makes too many assumptions about the execution environment?

  • Are there any browsers in which the above would fail, or is this considered standard behavior across the board?

Does anyone here know the answers to those questions and would be willing to enlighten me? I tried a quick internet search, but I admit I'm not sure how to even properly phrase the query. Pointers to references and documentation are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停滞 2024-09-08 20:50:23

这是仅适用于 IE 的非标准行为,不应使用。

[1] [2] [3]

This is a non-standard IE-only behavior and should not be used.

[1] [2] [3]

谈下烟灰 2024-09-08 20:50:23

这不是标准功能,仅 IE 支持,在某些情况下,Opera 支持。它还可能受到浏览器是否处于 怪异模式 以及元素是否处于有一个 name 属性。

对于第二点, rhino 本身不包含 DOM 实现,因此问题不适用。也就是说,没有内置HTML解释器,因此不会有额外的变量。

env.js 是 rhino 的 DOM 实现,提供 HTML 解释器。由于 ID 作为 window 范围变量不是 Web 标准,并且 env 项目旨在符合标准,因此您也不会在那里看到所描述的行为。

希望这有帮助!

This is not a standard feature, supported only by IE and, in some cases, Opera. It may also be affected by whether the browser is in quirks mode, and whether the element has a name attribute.

To your second point, rhino does not contain a DOM implementation itself, so the question does not apply. That is, there is no HTML interpreter built-in, so there will be no additional variables.

env.js is a DOM implementation for rhino, providing an HTML interpreter. Since IDs as window scope variables is not a web standard, and the env project aims to be standards compliant, you will not see the described behavior there either.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文