【问题标题】:what is the jQuery / javascript context of a frame within an iframe?iframe 中框架的 jQuery / javascript 上下文是什么?
【发布时间】:2011-01-10 03:44:36
【问题描述】:

让我以...作为开头...

Run JQuery in the context of another frame

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

<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...我还缺少什么?

【问题讨论】:

标签: javascript jquery iframe frameset


【解决方案1】:

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

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

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

关键是使用contents()函数。见Traversing/contents

【讨论】:

  • ahh contents() 确实很棒,我没有意识到提供的 iframe 支持...谢谢!
【解决方案2】:

我认为 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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 感谢您的反馈!但是,这对我来说是轰炸。 id 现在有下划线而不是 ndashes。如果我使用 console.log 来查看“幕后”发生的事情,如果我记录 window.top.this_iframe.document 或者如果我记录 window.top.document.getElementById('this_iframe'),它将返回一些东西,所以看起来frame_edit id 没有被“看到”。有什么想法吗?
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 2010-10-25
      • 2013-05-01
      • 2011-09-20
      相关资源
      最近更新 更多