在另一个框架的上下文中运行 JQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/539504/
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
Run JQuery in the context of another frame
提问by ChrisDiRulli
The client I am working with has a frameset like so...
我正在与之合作的客户有一个像这样的框架集......
<frameset rows="100,*, 0">
<frame name="theFrame" id="theFrame" src="blah.html" >
<frame name="theSecondFrame" id="theSecondFrame" src="foo.html" >
<frame name="importantFrame" id="importantFrame" src="myFrame.html" >
</frameset>
When a certain action takes place I need my frame (importantframe which is currently hidden) to mostly take over the page and block any interaction with the other frames. I'm planning on blocking interaction using the jquery block UI plugin.
当某个动作发生时,我需要我的框架(当前隐藏的重要框架)主要接管页面并阻止与其他框架的任何交互。我计划使用 jquery 块 UI 插件阻止交互。
The problem is I can't actually change the foo.html or blah.html files. So the JS code cannot live there. What I need to do is execute my jquery code in the context of those frames. So just to recap, I need my JQuery code to live in myFrame.html but execute in the context of the other frames. How can I do that? Hope that makes sense.
问题是我实际上无法更改 foo.html 或 blah.html 文件。所以JS代码不能存在于那里。我需要做的是在这些帧的上下文中执行我的 jquery 代码。所以只是回顾一下,我需要我的 JQuery 代码存在于 myFrame.html 中,但在其他框架的上下文中执行。我怎样才能做到这一点?希望这是有道理的。
Thanks CDR
谢谢 CDR
回答by pjb3
The jQuery
function, which you more commonly call with $
, takes a second argument called context, which is "which DOM element of jQuery object should I do the search in". Most of the time you omit this argument, so the context defaults to the current HTML document. When your code is executing in the iframe, the document defaults to that iframe's document. But you can easily grab the document for one of the other frames.
jQuery
您更常使用 调用的函数$
采用名为 context 的第二个参数,即“我应该在哪个 jQuery 对象的 DOM 元素中进行搜索”。大多数情况下,您会忽略此参数,因此上下文默认为当前 HTML 文档。当您的代码在 iframe 中执行时,文档默认为该 iframe 的文档。但是您可以轻松抓取其他框架之一的文档。
For example, put this in myFrame.html
, and this will remove all the h1 elements from the frame with blah.html
in it. Notice the second argument to $
, which is the expression that grabs the blah frame from within important frame:
例如,将其放入myFrame.html
,这将从其中删除框架中的所有 h1 元素blah.html
。注意 的第二个参数$
,它是从重要帧中抓取 blah 帧的表达式:
<html>
<head>
<script type="text/javascript" src="/javascripts/jquery.js"></script>
<script type="text/javascript">
function doIt() {
$('h1', window.parent.frames[0].document).remove()
}
</script>
</head>
<body>
<h1>My Frame</h1>
<a href="#" onclick="doIt()">Do It</a>
</body>
</html>
回答by Patrick Fisher
Like pjb3 said, set the jQuery context. If you have nested frames, your code will look like this:
就像 pjb3 说的,设置 jQuery 上下文。如果您有嵌套框架,您的代码将如下所示:
$('*',window.parent.frames[0].frames[0].document).size();
Or, better yet, make a shortcut:
或者,更好的是,创建一个快捷方式:
targetFrame = window.parent.frames[0].frames[0].document;
$('*',targetFrame).size();
回答by aliaksej
In versions of jQuery >=1.7 we cannot get events old way
在 jQuery >=1.7 的版本中,我们无法以旧方式获取事件
Older versions (<1.7):
旧版本(<1.7):
var events = $('#form').data('events');
1.7 and newer:
1.7 及更新版本:
var events = $.fn.data($('#form'), 'events');
回答by Andrew M
I'm not certain about the block plugin, but can't you get hold of the relevant frame and manipulate it using the "frame tree", something like this: (from within importantFrame.htm)
我不确定块插件,但你不能掌握相关的框架并使用“框架树”来操作它,就像这样:(来自importantFrame.htm)
parent.theFrame.someProperty = someValue;
eg:
例如:
parent.theFrame.location.href='anotherPage.html';
From what I've been reading, you might need Jquery on the target page, but you could try something like:
根据我一直在阅读的内容,您可能需要在目标页面上使用 Jquery,但您可以尝试以下操作:
parent.theFrame.block();
or maybe:
或者可能:
$('#theFrame').block();