jQuery:查找元素,无论框架如何
我们的开发网站和公司网站的框架层次结构略有不同。
偶尔我需要引用不同框架中的元素。
不幸的是,我不知道该框架在层次结构中的位置。不过我知道它是 id。
当我不知道确切的结构时,如何找到框架或特定元素?
澄清一下 在开发中,它基本上只是一个页面,包含一个框架。我在框架的上下文中,只是使用 window.top.document 来获取我想要的文档。
在产品中,嵌套是不同的,window.top.document 不起作用,因为它位于其他两个框架内。
我无法更改此结构,这就是我们的应用程序门户的工作方式。
Our dev and corporate sites have a slightly different frame hierarchy.
Very occasionally I need to reference an element in a different frame.
Unfortunately, I don't know where that frame will be in the hierarchy. I do however know it's id.
How can I find a frame or the specific element, when I don't know the exact structure?
For clarification
In dev, it's basically just a page, containing a frame. I'm in the context of the frame, and was just using window.top.document to get the document I wanted.
In prod, the nesting is different, window.top.document doesn't work, as it is inside of two other frames.
I can't change this structure, it's just how our application portal works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 iframe 来自同一个域,则可以通过 jQuery 轻松访问元素,因为
关于外部域有一些例外 这里 :
If the iframe is from the same domain, the elements are easily accessible via jQuery as
There are a few exceptions Here about foreign domain:
也许你可以尝试遍历所有框架。您将需要使用广度优先或深度优先搜索,具体取决于框架嵌套的深度。最初将根传递给它。
我不能 100% 确定这个递归算法的正确性,但我相信这是正确的想法。
编辑:
如果您需要找到最顶层的父级,请尝试:
这将首先向上检查,如果不起作用则向下检查
maybe you could try traversing all the frames. you will either need to use a breadth-first or depth-first search depending on how deep the frame nesting will be. pass it the root originally.
I'm not 100% sure of the correctness of this recursive algorithm but this is the right idea I believe.
EDIT:
if you need to find the topmost parent, try:
this will check upwards first, then downwards if that doesn't work
如果您知道框架的 id,则可以使用 id 选择器和
contents()
方法来查找 iframe 中的元素。试试这个If you know the id of the frame you can just use id selector and
contents()
method to find the elements within the iframe. Try this这对我有用......
this worked for me...