0

ページのコンテンツに合わせて iFrame の高さを動的にしようとしています。

2ページあります。index.php と example.html

「index.php」に「example.html」をiFrameとして挿入すると、「example.html」のコンテンツの高さが変更可能になります。

これは「index.php」の私のコードです

<iframe src="example.html" name="iframe1" id="myiframe" marginwidth="0"
marginheight="0" align="top" scrolling="no" width="100%" height="auto" frameborder="0">
</iframe>

<script type="text/javascript">
var f = document.getElementById("myiframe");
f.onload = function() {
  this.style.height = this.contentWindow.document.body.offsetHeight + "40px";
}
</script>

私のCSS:

#myiframe {
margin: auto;
padding: 0px;
float: left;
height: auto;
min-height: 255px;
height: auto !important;        /* for IE as it does not support min-height */
height: 255px;                   /* for IE as it does not support min-height */
width: 100%;
}

iFrame は正常に機能しますが、動的な高さはまったく機能しません。助言がありますか?ありがとう。

4

2 に答える 2

1

jQuery では、これを親ページに配置します。

$('iframe').load(function () {
    $(this).height($(this).contents().height());
    $(this).width($(this).contents().width());
});

それらが異なるサブドメインである場合は、これを親ページと iframe ページの両方に追加する必要があります。

    document.domain = "shareddomain.com"
于 2013-11-13T18:09:47.300 に答える