iframe 中框架的 jQuery / javascript 上下文是什么?

发布于 2024-08-19 13:30:31 字数 787 浏览 6 评论 0原文

让我以此作为序言...我已经引用了这个问题/答案,它似乎包含线索,但我仍然缺少整个图片

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

本质上,索引页的结构就是这个

<html>
<body>
  <div class="frames-wrap">
      <iframe id="this-iframe" src="location.php">

      </iframe>
  </div>
</body>
</html>

location.php 然后包含一个框架集(咳咳,不是我的想法...)有两个框架,设置如下...

<frameset cols="350,*">
  <frame src="search.php" id="frame_search" name="search"/>
  <frame src="edit.php" id="frame_edit" name="edit" />
</frameset>  

如果我想在索引页和这些元素之间操作对象,我将如何处理?

我一直认为上下文应该类似于 window.parent.frames[0].document...我还缺少什么?

Let me preface this with... I have referenced this question/answers and it seems to contain clues, but i'm still missing the whole picture

Run JQuery in the context of another frame

Essentially, the structure of the index page is this

<html>
<body>
  <div class="frames-wrap">
      <iframe id="this-iframe" src="location.php">

      </iframe>
  </div>
</body>
</html>

location.php then contains a frameset (ahem, not my idea...) that has two frames that are set up like so...

<frameset cols="350,*">
  <frame src="search.php" id="frame_search" name="search"/>
  <frame src="edit.php" id="frame_edit" name="edit" />
</frameset>  

if i want to manipulate objects between the index page and these elements how would i go about this?

I keep thinking the context should be something similar to window.parent.frames[0].document... what else am i missing?

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

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

发布评论

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

评论(4

凉城 2024-08-26 13:30:31

前言:除非 iframe 内容来自同一域,否则您将无法访问它。

要选择 iframe 中的元素,您可以使用像这样的 jQuery 调用,

element = $("#this_iframe").contents().find("#frame_search")

关键是使用 contents() 函数。请参阅遍历/contents

Preface: You wont be able to access the iframes contents unless it originates from the same domain.

To select elements in your iframe you could use a jQuery call like this

element = $("#this_iframe").contents().find("#frame_search")

The key is to use the contents() function. See Traversing/contents

没企图 2024-08-26 13:30:31

我认为来自 technicolorenvy 的链接有答案,但选择器有一个鲜为人知的第二个参数,您可以在其中设置上下文。

像这样的东西:

var iframeDoc = document.getElementById('myIframe');
iframeDoc = (iframeDoc.contentWindow) ? iframeDoc.contentWindow : (iframeDoc.contentDocument.document) ? iframeDoc.contentDocument.document : iframeDoc.contentDocument;


// From the parent window
$('p', iframeDoc).html('Hello from parent');

http://docs.jquery.com/Core/jQuery#expressioncontext

I think the link from technicolorenvy has the answer, but the selector has a lesser known second parameter where you can set the context.

Something like this:

var iframeDoc = document.getElementById('myIframe');
iframeDoc = (iframeDoc.contentWindow) ? iframeDoc.contentWindow : (iframeDoc.contentDocument.document) ? iframeDoc.contentDocument.document : iframeDoc.contentDocument;


// From the parent window
$('p', iframeDoc).html('Hello from parent');

http://docs.jquery.com/Core/jQuery#expressioncontext

我的黑色迷你裙 2024-08-26 13:30:31

为您的框架提供有效的 JavaScript 标识符会有所帮助,然后您可以使用诸如 window.top.this_iframe.frame_edit.document 之类的结构作为上下文。

Giving your frames ids that are valid JavaScript identifiers would help, then you could use constructs such as window.top.this_iframe.frame_edit.document as your context.

·深蓝 2024-08-26 13:30:31

这些都很有帮助。当我试图通过 DOM 中的 iframe 时,我一直在轰炸。这似乎是因为我的代码驻留在 ready() 方法中,但在 iframe 中调用的框架集在 $(document).ready() 触发时尚未加载。

感谢所有的大力帮助和反馈!

These were all helpful. I kept bombing when I was attempting to get past the iframe in the DOM. THis would appear to be from the fact i had code residing in the ready() method, but the frameset that was being called within the iframe was not loaded by the time that had $(document).ready() fired.

Thanks for all the great help and feedback!

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