iframe 是否有可能在没有主窗口帮助的情况下调整自身大小? [英] is it possible that iframe resize itself without help from main window?

查看:22
本文介绍了iframe 是否有可能在没有主窗口帮助的情况下调整自身大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主窗口和 iframe 都在同一个域中,但我想要实现的是仅在该 iframe 内使用 js 调整 iframe 的大小以适应其内容.

Both main window and iframe are on the same domain, but what I want to achieve is to resize iframe to fit its contents using js only within that iframe.

我也不知道主窗口分配给它的 iframe 的 ID.我不想使用 jquery 或任何其他框架.

I also do not know the id of the iframe that is assigned to it by main window. And I do not want to use jquery or any other framework for it.

推荐答案

您也可以在不知道父窗口中 iframeid 的情况下执行此操作:

You can do this also without knowing the idof the iframe in the parent window:

window.frameElement.style.width = iframeContentWidth + 'px';
window.frameElement.style.height = iframeContentHeight + 'px';

MDNframeElement>.

See frameElement at MDN.

编辑

万一 iframe 恰好位于具有固定大小和 overflow: hidden 的容器元素中,您可以执行以下操作:

Just in case the iframe happens to be in a container element which has fixed size and overflow: hidden you can do something like this:

function resizeIframe (iframeContentWidth, iframeContentHeight) {
    var container = window.frameElement.parentElement;
    if (container != parent.document.body) {
        container.style.width = iframeContentWidth + 'px';
        container.style.height = iframeContentHeight + 'px';
    }
    window.frameElement.style.width = iframeContentWidth + 'px';
    window.frameElement.style.height = iframeContentHeight + 'px';
    return;
}

这篇关于iframe 是否有可能在没有主窗口帮助的情况下调整自身大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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