在jQuery中检索文档的父iframe [英] Retrieving a document's parent iframe in jQuery

查看:104
本文介绍了在jQuery中检索文档的父iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中包含一个iframe,其中包含一个html文档。我需要从该子文档中访问iframe的id,并且没有运气使用jQuery。基本结构是在我的页面中:

 < div id =ContainingDiv> 
< iframe id =ContainingiFrame>
< html>
< head>< / head>
< body>
< / body>
< / html>
< / iframe>
< / div>

在我的javascript方法中,我可以得到< body> 使用jQuery选择器的标签,但是无法检索iframe元素以便调整其大小。我已经尝试了一些东西,但我承认不是jQuery DOM导航的专家,特别是在涉及iframe的情况下(iframe与包含页面在同一个域中)有没有办法可以做到这一点?

解决方案

根本不需要jQuery。要获取父母的身体对象,您可以执行以下操作:

  var parentBody = window.parent.document.body 

如果它与您运行代码的iframe位于同一个域,一旦你有可以在该对象上使用普通的javascript:

  window.parent.document.getElementById(ContainingiFrame)。style.height = 400像素; 

或使用jQuery:

  $(#ContainingiFrame,parentBody).height(400); 

这里是一篇关于通过示例代码调整iframe中的iframe大小的文章: http://www.pither.com/articles/2010/11/12/ resize-iframe-from-within



而且,根据自己的内容调整iframe大小的相关问题/答案:根据内容调整iframe


I have a page with an iframe that has an html document within it. I need to access the iframe's id from that child document, and am having no luck getting at it with jQuery. The essential structure is this within my page:

<div id="ContainingDiv">
  <iframe id="ContainingiFrame">
   <html>
     <head></head>
     <body>
     </body>
   </html>
  </iframe>
</div>

In my javascript method I can get to anything in the <body> tags using jQuery selectors, but cannot retrieve the iframe element in order to resize it. I've tried a few things but am admittedly not an expert in jQuery DOM navigation, particularly where iframes are involved (the iframe is in the same domain as the containing page.) Is there a way I can do this?

解决方案

No need for jQuery at all. To get the body object of your parent, you can do this:

var parentBody = window.parent.document.body

If it's on the same domain as your iframe that you are running the code from, once you have that, you can use normal javascript on that object:

window.parent.document.getElementById("ContainingiFrame").style.height = "400px";

or with jQuery:

$("#ContainingiFrame", parentBody).height("400");

Here's an article on resizing an iframe from within the iframe with sample code: http://www.pither.com/articles/2010/11/12/resize-iframe-from-within

And, a related question/answer on resizing an iframe based on it's own content: Resizing an iframe based on content

这篇关于在jQuery中检索文档的父iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆