如何使用 JavaScript 获取 HTML 节点所属的窗口对象?

发布于 2024-07-07 06:57:15 字数 144 浏览 10 评论 0原文

由于存在多个 iframe、XUL 浏览器元素等,我的 XULRunner 应用程序中有许多窗口对象。 我正在寻找使用 JavaScript 查找指定节点所属的窗口对象的最佳方法。

因此,更具体地说,给定节点 x,我需要找到包含 x 的特定窗口对象。

Because of several iframes, XUL browser elements, and so forth, I have a number of window objects in my XULRunner application. I'm looking for the best way to find the window object that a specified node belongs to using JavaScript.

So, to be more specific, given node x, I need to find the specific window object that contains x.

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

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

发布评论

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

评论(3

豆芽 2024-07-14 06:57:15

对你的问题+1,这正是我正在寻找的,感谢您通过回答自己直接给出的提示。

我用谷歌搜索了一下,根据 http://www.quirksmode.org/dom/w3c_html.html< /a> 跨浏览器表 我认为正确的答案是:

function GetOwnerWindow(html_node)
{
   /*
   ownerDocument is cross-browser, 
   but defaultView works on all browsers except Opera/IE that use parentWinow
   */
   return (html_node.ownerDocument.defaultView) ?
      html_node.ownerDocument.defaultView : 
      html_node.ownerDocument.parentWindow;
}

或者甚至更好:

return html_node.ownerDocument.defaultView || html_node.ownerDocument.parentWindow;

请让我知道您的想法。

+1 to your question, it was exactly what I was looking for and thanks for the hint given directly by answering yourself.

I Googled a bit and according to http://www.quirksmode.org/dom/w3c_html.html cross-browsers tables I think the right answer is:

function GetOwnerWindow(html_node)
{
   /*
   ownerDocument is cross-browser, 
   but defaultView works on all browsers except Opera/IE that use parentWinow
   */
   return (html_node.ownerDocument.defaultView) ?
      html_node.ownerDocument.defaultView : 
      html_node.ownerDocument.parentWindow;
}

Or maybe even better:

return html_node.ownerDocument.defaultView || html_node.ownerDocument.parentWindow;

Plz let me know your thoughts.

猥︴琐丶欲为 2024-07-14 06:57:15

我找到了我想要的属性组合:

node.ownerDocument.defaultView

它返回节点所属的窗口对象。 请注意,这在 IE 中不起作用。

I found the combination of properties I was after:

node.ownerDocument.defaultView

That returns the window object that the node belongs to. Note that this does not work in IE.

绅刃 2024-07-14 06:57:15

您可能想使用self。 self 是对当前文档的引用。

从 iframe 中:

<body>
<div id="example">Example!</div>
<script type="text/javascript">
    window.onload = function () {
        var exampleNode = document.getElementById('example');
        exampleNode.bar = function () {
            // The highest window object:
            top;
            // The parent node:
            self;
            // The parent node ( except in IE );
            this.ownerDocument.defaultView;
        };
    };
</script>
</body>

遍历多个窗口对象:

在浏览器对象模型中,主窗口对象称为 top。 其他全局对象按从顶部开始的树结构排列。 通过引用top,您可以使用它们的名称和关系导航到树中的其他全局对象,这与您遍历DOM。

当您有多个窗口对象时,就像在 iframe(旧式框架集)的情况下一样,框架具有名称属性。 给定对象相对于顶部窗口对象的位置,您可以使用子窗口的名称来访问该对象。

然后从顶部窗口的上下文中:

self.advertisement

You may want to use self. self is a reference to the current document.

From within the iframe:

<body>
<div id="example">Example!</div>
<script type="text/javascript">
    window.onload = function () {
        var exampleNode = document.getElementById('example');
        exampleNode.bar = function () {
            // The highest window object:
            top;
            // The parent node:
            self;
            // The parent node ( except in IE );
            this.ownerDocument.defaultView;
        };
    };
</script>
</body>

Traversing Multiple Window Objects:

Within the browser object model, the primary window object is referred to as top. Other global objects are arranged in a tree structure which stems from top. With a reference to top, you can navigate to the other global objects in the tree by using their names and relationships, much in the same way as you traverse the DOM.

When you have multiple window objects, as you do in the case of an iframe (of with old school framesets), the frame has a name attribute. Given the objects position relative to the top window object, you can use the name of the child to access the object.

<iframe src ="/default.html" name="advertisement"></iframe>

and then from the context of the top window:

self.advertisement

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