使用 jQuery 获取 Frame 中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8779728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting element within Frame using jQuery
提问by Jeff Fol
I am trying to access an element which is inside of a frame but haven't had any luck getting to it so far. I have read through a lot of examples here on stackoverflow and in jQuery's documentation but all the examples I've seen have been in reference to iFrames which behave differently than traditional frames. My page structure is as shown below with actual contents removed:
我正在尝试访问框架内的元素,但到目前为止还没有任何运气。我已经阅读了很多关于 stackoverflow 和 jQuery 文档的示例,但我看到的所有示例都参考了 iFrame,它们的行为与传统框架不同。我的页面结构如下所示,删除了实际内容:
<html>
<head></head>
<frameset>
<frame name="Menu"><html><body>
<!--Menu contents-->
</body></html></frame>
<frameset>
<frame name="SettingsTree"><html><body>
<!--Setting options-->
</body></html></frame>
<frame name="SettingsGrid" id="SettingsGrid"><html><body>
<div id="findthis"></div>
<!--Setting grid values-->
</body></html></frame>
</frameset>
</frameset>
</html>
The way to get contents of "findthis" within an iFrame would be
在 iFrame 中获取“findthis”内容的方法是
$('#SettingsGrid').contents().find('#findthis')
however this doesn't return anything. The $('#SettingsGrid') object exists with a length of 1 and all of the html I would expect it to have. However when I call .contents() on that object it returns nothing. I don't know if this is because it hasn't been properly loaded into the DOM or if there is some other issue.
然而这不会返回任何东西。$('#SettingsGrid') 对象存在的长度为 1 以及我希望它拥有的所有 html。但是,当我在该对象上调用 .contents() 时,它不返回任何内容。我不知道这是因为它没有正确加载到 DOM 中,或者是否存在其他问题。
回答by dgilland
Try this:
尝试这个:
$('#findthis', window.parent.frames[0].document)
Or from the top level:
或者从顶层:
$('#findthis', top.window.frames[0].document)
See previous question/answer: Run JQuery in the context of another frame
请参阅上一个问题/答案:在另一个框架的上下文中运行 JQuery
回答by James Drinkard
This worked for me, if the frame has a name as well as an id:
如果框架有名称和 ID,这对我有用:
$('#frame_id',top.frames["frame_name"].document) ...
回答by DoronG
My best working solution on IE8 / IE11 Enterprise mode with jQuery 1.11.1 is:
我使用 jQuery 1.11.1 在 IE8 / IE11 企业模式上的最佳工作解决方案是:
$('#findthis', $('#SettingsGrid')[0].contentWindow.document)
回答by Karol
For me good way to do this was to find a frame by class, and than use jquery`s prop() method and contentwindow. Unfortunately in this frame must be declared by some function which does a tasks for you e.g. findAndsubmitForms() method.
对我来说,这样做的好方法是按类查找框架,而不是使用 jquery 的 prop() 方法和 contentwindow。不幸的是,在此框架中必须由某些为您执行任务的函数声明,例如 findAndsubmitForms() 方法。
parent window:
父窗口:
$('.frameClass or #id or so').prop('contentWindow').findAndSubmitForms();
child window (frame):
子窗口(框架):
function findAndSubmitForms(){
$('form').submit();
}
If this is not possible to declare such method inside frame it of course could be done by defining it like this:
如果不可能在框架内声明这样的方法,当然可以通过如下定义来完成:
$('.frameClass').prop('contentWindow').findAndSubmitForms=function(){...};
and than call our desired function :d
然后调用我们想要的函数:d
have a nice day and wish you a great time looking at screen :D
祝你有美好的一天,祝你看屏幕玩得开心:D
回答by Fortunato
Have you tried $('#findthis', $('#SettingsGrid'))
?
你试过$('#findthis', $('#SettingsGrid'))
吗?
The second argument within the outer jquery is the context in which to make the search referred to by the first argument. When no second argument is given then it is by default the root.
外部 jquery 中的第二个参数是进行第一个参数引用的搜索的上下文。当没有给出第二个参数时,默认情况下它是根。