Javascript 如何制作动态iFrame高度?

Javascript 如何制作动态iFrame高度?,javascript,php,html,css,iframe,Javascript,Php,Html,Css,Iframe,我试图使动态iFrame的高度,以适应页面的内容 我有两页。index.php和example.html 我在“index.php”中插入“example.html”作为iFrame,“example.html”中的内容高度是可变的 这是我在“index.php”中的代码 iFrame工作正常,但动态高度根本不起作用。有什么建议吗?谢谢。您可以使用jquery,它可以通过使用$(window).height()完成 嗨 $('.myIframe').css('height',$(window.

我试图使动态iFrame的高度,以适应页面的内容

我有两页。index.php和example.html

我在“index.php”中插入“example.html”作为iFrame,“example.html”中的内容高度是可变的

这是我在“index.php”中的代码


iFrame工作正常,但动态高度根本不起作用。有什么建议吗?谢谢。

您可以使用jquery,它可以通过使用$(window).height()完成

$('.myIframe').css('height',$(window.height()+'px');
使用jQuery,将其放在父页面中

$('iframe').load(function () {
    $(this).height($(this).contents().height());
    $(this).width($(this).contents().width());
});
如果它们是不同的子域,则需要将其添加到父级和iframe页面:

    document.domain = "shareddomain.com"

两个页面都在同一个域上吗?是否存在任何JavaScript安全警告?是的,它们都在线托管在相同的域和路径上@Grimmus您使用的是什么浏览器?iframe onload()支持在不同浏览器中的质量不同。例如,我只在Chrome和Mozilla上测试了@axel_cI'd do in
iframe
<iframe src="example.html" width="100%" class="myIframe">
<p>Hi</p>
</iframe>

<script type="text/javascript" language="javascript"> 
$('.myIframe').css('height', $(window).height()+'px');
</script>
$('iframe').load(function () {
    $(this).height($(this).contents().height());
    $(this).width($(this).contents().width());
});
    document.domain = "shareddomain.com"