如何使用 JavaScript 获取 HTML 节点所属的窗口对象?
由于存在多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对你的问题+1,这正是我正在寻找的,感谢您通过回答自己直接给出的提示。
我用谷歌搜索了一下,根据 http://www.quirksmode.org/dom/w3c_html.html< /a> 跨浏览器表 我认为正确的答案是:
或者甚至更好:
请让我知道您的想法。
+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:
Or maybe even better:
Plz let me know your thoughts.
我找到了我想要的属性组合:
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.
您可能想使用self。 self 是对当前文档的引用。
从 iframe 中:
遍历多个窗口对象:
在浏览器对象模型中,主窗口对象称为 top。 其他全局对象按从顶部开始的树结构排列。 通过引用top,您可以使用它们的名称和关系导航到树中的其他全局对象,这与您遍历DOM。
当您有多个窗口对象时,就像在 iframe(旧式框架集)的情况下一样,框架具有名称属性。 给定对象相对于顶部窗口对象的位置,您可以使用子窗口的名称来访问该对象。
然后从顶部窗口的上下文中:
self.advertisement
You may want to use self. self is a reference to the current document.
From within the iframe:
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