如何更改其内容的 Iframe 高度(两者都来自同一域)

How to change the Iframe height from its content (both from same domain)

本文关键字:两者都 高度 何更改 Iframe      更新时间:2023-09-26

我想从这个 iframe 的内容动态更改 iframe 元素的高度,这是我的 js:

function iframeLoaded()
{
    var iframeID = window.parent.document.getElementById('iframeContainer');
    if(iframeID) {
        console.log('if');
        iframeID.height = "";
        iframeID.height = iframeID.contentWindow.document.body.scrollHeight + "px";
    }
}

每当内容高度发生变化时,我都会运行此功能。而且我有错误:访问属性"文档"的权限被拒绝

这有什么问题。父网站和子网站在同一个多明!

父级 http://example.com html 代码的 iframe 部分:

<iframe id="iframeContainer" class="wrapper" width="100%" scrolling="no" height="700" frameborder="1" src="http://example.com/ext/index.php" name="iframe">...</iframe>
我认为可以

反过来做。您可以从 iframe 触发父级。像这样的东西(如果你使用jQuery):

parent.$('body').trigger( 'eventName' );

也看看这个前面的线程:从 iframe 触发的事件可以由其父级中的元素处理吗?

顺便说一下,你写了"每当内容高度发生变化时,我都会运行这个函数"。 你现在如何获得这种变化?

对不起,我的错误它不是同一个域(一个有 www,另一个没有)=/