动态调整 iframe 的高度

发布于 2024-12-07 02:51:31 字数 752 浏览 3 评论 0原文

我有一个 iframe,其中包含网站的一些内容。我希望 iframe 调整为 src 内容高度的 100%。 js 方面的菜鸟 - 这就是我正在处理的内容:

  <iframe id="frame" scrolling="no" frameborder="0" src="http://www.srcwebsite.org"></iframe>

    <script type="text/javascript">
    function resizeIframe() {
        var height = document.documentElement.clientHeight;
        height -= document.getElementById('frame').offsetTop;

        height -= 20; /* whatever you set your body bottom margin/padding to be */

        document.getElementById('frame').style.height = height +"px";

    };
    document.getElementById('frame').onload = resizeIframe;
    window.onresize = resizeIframe;
    </script>

这对于调整 iframe 的高度到其页面的高度非常有用,但我希望它将其调整到 src 的高度......

I have an iframe that contains some content from a site. I want the iframe to adjust to 100% of the src content's height. Bit of a noob at js - here's what I'm working with:

  <iframe id="frame" scrolling="no" frameborder="0" src="http://www.srcwebsite.org"></iframe>

    <script type="text/javascript">
    function resizeIframe() {
        var height = document.documentElement.clientHeight;
        height -= document.getElementById('frame').offsetTop;

        height -= 20; /* whatever you set your body bottom margin/padding to be */

        document.getElementById('frame').style.height = height +"px";

    };
    document.getElementById('frame').onload = resizeIframe;
    window.onresize = resizeIframe;
    </script>

This works great to resize the height of the iframe to its page, but I want it to resize it to the height of the src...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2024-12-14 02:51:31

由于同源政策,您无法访问 iframe 内容的高度(协议、域和端口必须匹配)。

您可以设置 CORS,请记住,在 IE8 之前,IE 不支持它。

You can't access the height of the iframe's content because of Same Origin Policy (protocol, domain and ports must match).

You could set up CORS, keep in mind it's not supported in IE until IE8.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文