iframe 中框架的 jQuery / javascript 上下文是什么?
让我以此作为序言...我已经引用了这个问题/答案,它似乎包含线索,但我仍然缺少整个图片
本质上,索引页的结构就是这个
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
前言:除非 iframe 内容来自同一域,否则您将无法访问它。
要选择 iframe 中的元素,您可以使用像这样的 jQuery 调用,
关键是使用
contents()
函数。请参阅遍历/contentsPreface: 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
The key is to use the
contents()
function. See Traversing/contents我认为来自 technicolorenvy 的链接有答案,但选择器有一个鲜为人知的第二个参数,您可以在其中设置上下文。
像这样的东西:
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:
http://docs.jquery.com/Core/jQuery#expressioncontext
为您的框架提供有效的 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.这些都很有帮助。当我试图通过 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!