jquery在加载时获取iframe内容的高度

FFi*_*ish 76 iframe jquery height onload

我有一个帮助页面,help.php,我加载一个iframe里面main.php我怎样才能得到这个页面的高度,一旦它在iframe中加载?

我问这个是因为我无法将iframe的高度设置为100%或auto.这就是为什么我认为我需要使用javascript ..我正在使用jQuery

CSS:

body {
    margin: 0;
    padding: 0;
}
.container {
    width: 900px;
    height: 100%;
    margin: 0 auto;
    background: silver;
}
.help-div {
    display: none;
    width: 850px;
    height: 100%;
    position: absolute;
    top: 100px;
    background: orange;
}
#help-frame {
    width: 100%;
    height: auto;
    margin:0;
    padding:0;
}
Run Code Online (Sandbox Code Playgroud)

JS:

$(document).ready(function () {
    $("a.open-help").click(function () {
        $(".help-div").show();
        return false;
    })
})
Run Code Online (Sandbox Code Playgroud)

HTML:

<div class='container'>
    <!-- -->
    <div class='help-div'>
        <p>This is a div with an iframe loading the help page</p>
        <iframe id="help-frame" src="../help.php" width="100%" height="100%" frameborder="1"></iframe>
    </div>  <a class="open-help" href="#">open Help in iFrame</a>

    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
    <p>hello world</p>
</div>
Run Code Online (Sandbox Code Playgroud)

FFi*_*ish 100

好吧我终于找到了一个好的解决方案

$('iframe').load(function() {
    this.style.height =
    this.contentWindow.document.body.offsetHeight + 'px';
});
Run Code Online (Sandbox Code Playgroud)

因为一些浏览器(较旧的Safari和Opera)报告onload在CSS渲染之前完成,你需要设置一个微超时并空白并重新分配iframe的src.

$('iframe').load(function() {
    setTimeout(iResize, 50);
    // Safari and Opera need a kick-start.
    var iSource = document.getElementById('your-iframe-id').src;
    document.getElementById('your-iframe-id').src = '';
    document.getElementById('your-iframe-id').src = iSource;
});
function iResize() {
    document.getElementById('your-iframe-id').style.height = 
    document.getElementById('your-iframe-id').contentWindow.document.body.offsetHeight + 'px';
}
Run Code Online (Sandbox Code Playgroud)

  • 这仅适用于同一域上的页面 (121认同)
  • 当我运行此命令时,我收到“未捕获的 DOMException:访问跨源对象上的属性“文档”的权限被拒绝” (4认同)
  • 我相信load事件在同一个事件循环上触发,因此setTimout为1毫秒也可以正常工作 (2认同)

And*_*rew 45

不那么复杂的答案就是.contents()用来获取iframe.有趣的是,我相信,由于身体上的填充,它返回的值与我在原始答案中使用代码的值不同.

$('iframe').contents().height() + 'is the height'
Run Code Online (Sandbox Code Playgroud)

这就是我为跨域通信所做的工作,所以我担心它可能会不必要地复杂化.首先,我将jQuery放在iFrame的文档中; 这将消耗更多内存,但它不应该增加加载时间,因为脚本只需要加载一次.

使用iFrame的jQuery尽可能早地测量iframe体的高度(onDOMReady),然后将URL哈希值设置为该高度.在父文档中,onload向iFrame标记添加一个事件,该标记将查看iframe的位置并提取您需要的值.因为onDOMReady将始终在文档的加载事件之前发生,所以您可以相当确定该值将正确地传达,而不会出现竞争条件使事情复杂化.

换一种说法:

...在Help.php中:

var getDocumentHeight = function() {
    if (location.hash === '') { // EDIT: this should prevent the retriggering of onDOMReady
        location.hash = $('body').height(); 
        // at this point the document address will be something like help.php#1552
    }
};
$(getDocumentHeight);
Run Code Online (Sandbox Code Playgroud)

......并在父文件中:

var getIFrameHeight = function() {
    var iFrame = $('iframe')[0]; // this will return the DOM element
    var strHash = iFrame.contentDocument.location.hash;
    alert(strHash); // will return something like '#1552'
};
$('iframe').bind('load', getIFrameHeight );
Run Code Online (Sandbox Code Playgroud)


Jac*_*les 21

我发现以下内容适用于Chrome,Firefox和IE11:

$('iframe').load(function () {
    $('iframe').height($('iframe').contents().height());
});
Run Code Online (Sandbox Code Playgroud)

完成Iframes内容后,将触发该事件,并将IFrames高度设置为其内容的高度.这仅适用于与IFrame相同的域中的页面.


Rka*_*ght 6

你不需要iframe里面的jquery来做这件事,但是我使用它导致代码变得如此简单......

将它放在iframe中的文档中.

$(document).ready(function() {
  parent.set_size(this.body.offsetHeight + 5 + "px");  
});
Run Code Online (Sandbox Code Playgroud)

在上面添加了五个以消除小窗口上的滚动条,它在尺寸上永远不会完美.

这在你的父文档中.

function set_size(ht)
{
$("#iframeId").css('height',ht);
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这不适用于跨域iframe (3认同)

cch*_*ain 6

没有jQuery这样做的代码现在是微不足道的:

const frame = document.querySelector('iframe')
function syncHeight() {
  this.style.height = `${this.contentWindow.document.body.offsetHeight}px`
}
frame.addEventListener('load', syncHeight)
Run Code Online (Sandbox Code Playgroud)

解开事件:

frame.removeEventListener('load', syncHeight)
Run Code Online (Sandbox Code Playgroud)

  • 是的,运气不好。我会为此利用 iframe-resizer 库。当然,您需要控制这两个域才能使其正常工作。 (3认同)
  • 漂亮又简单!不幸的是,不适用于跨域iframe :( (2认同)

小智 5

这是对我有用的正确答案

$(document).ready(function () {
        function resizeIframe() {
            if ($('iframe').contents().find('html').height() > 100) {
                $('iframe').height(($('iframe').contents().find('html').height()) + 'px')
            } else {
                setTimeout(function (e) {
                    resizeIframe();
                }, 50);
            }
        }
        resizeIframe();
    });
Run Code Online (Sandbox Code Playgroud)


Pix*_*omo 5

simple one-liner starts with a default min-height and increases to contents size.

<iframe src="http://url.html" onload='javascript:(function(o){o.style.height=o.contentWindow.document.body.scrollHeight+"px";}(this));' style="height:200px;width:100%;border:none;overflow:hidden;"></iframe>
Run Code Online (Sandbox Code Playgroud)