在另一个框架的上下文中运行 JQuery

发布于 2024-07-13 16:56:17 字数 627 浏览 5 评论 0原文

我正在使用的客户端有一个像这样的框架集...

<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>

当发生某个操作时,我需要我的框架(当前隐藏的重要框架)主要接管页面并阻止与其他框架的任何交互。 我计划使用 jquery block UI 插件来阻止交互。

问题是我实际上无法更改 foo.html 或 blah.html 文件。 所以 JS 代码不能驻留在那里。 我需要做的是在这些框架的上下文中执行我的 jquery 代码。 回顾一下,我需要将 JQuery 代码放在 myFrame.html 中,但在其他框架的上下文中执行。 我怎样才能做到这一点? 希望这是有道理的。

谢谢 CDR

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.

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.

Thanks
CDR

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

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

发布评论

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

评论(4

纸伞微斜 2024-07-20 16:56:17

jQuery 函数(您通常使用 $ 调用)采用名为 context 的第二个参数,即“我应该在 jQuery 对象的哪个 DOM 元素中进行搜索”。 大多数情况下,您会忽略此参数,因此上下文默认为当前 HTML 文档。 当您的代码在 iframe 中执行时,文档默认为该 iframe 的文档。 但您可以轻松抓取其他框架之一的文档。

例如,将其放入 myFrame.html 中,这将从包含 blah.html 的框架中删除所有 h1 元素。 请注意 $ 的第二个参数,它是从重要帧中获取 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>

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.

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:

<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>
秋风の叶未落 2024-07-20 16:56:17

就像 pjb3 所说,设置 jQuery 上下文。 如果您有嵌套框架,您的代码将如下所示:

$('*',window.parent.frames[0].frames[0].document).size();

或者,更好的是,创建一个快捷方式:

targetFrame = window.parent.frames[0].frames[0].document;
$('*',targetFrame).size();

Like pjb3 said, set the jQuery context. If you have nested frames, your code will look like this:

$('*',window.parent.frames[0].frames[0].document).size();

Or, better yet, make a shortcut:

targetFrame = window.parent.frames[0].frames[0].document;
$('*',targetFrame).size();
暮年 2024-07-20 16:56:17

我不确定块插件,但是您不能获取相关框架并使用“框架树”来操作它,如下所示:(来自 importantFrame.htm 内)例如

parent.theFrame.someProperty = someValue;

parent.theFrame.location.href='anotherPage.html';

来自我一直以来的情况阅读中,您可能需要在目标页面上使用 Jquery,但您可以尝试以下操作:

parent.theFrame.block();

或者也许:

$('#theFrame').block();

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)

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:

parent.theFrame.block();

or maybe:

$('#theFrame').block();
梦归所梦 2024-07-20 16:56:17

在 jQuery >=1.7 的版本中,我们无法以旧方式获取事件

旧版本 (<1.7):

var events = $('#form').data('events');

1.7 及更新版本:

var events = $.fn.data($('#form'), 'events');

In versions of jQuery >=1.7 we cannot get events old way

Older versions (<1.7):

var events = $('#form').data('events');

1.7 and newer:

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