动态iframe高度取决于PHP内容?

动态iframe高度取决于PHP内容?,php,html,Php,Html,如何将Iframe高度设置为其内容的动态高度 内容只是PHP代码,没有其他内容 我使用php查询数据库,然后显示一些广告。广告越多,iframe应该越高 我认为身高class='100%'应该这样做,但没有 我读过其他的Q,但他们的解决方案对我不起作用 建议 谢谢如果广告只是文本,您可以使用行高*行数作为高度。我假设您知道字体大小,通过计算的行数可以找到行数: <?php // $content is your ads // assuming ads.php returns an ad w

如何将Iframe高度设置为其内容的动态高度

内容只是PHP代码,没有其他内容

我使用php查询数据库,然后显示一些广告。广告越多,iframe应该越高

我认为身高class='100%'应该这样做,但没有

我读过其他的Q,但他们的解决方案对我不起作用

建议


谢谢

如果广告只是文本,您可以使用
行高*行数
作为高度。我假设您知道字体大小,通过计算

的行数可以找到行数:

<?php
// $content is your ads
// assuming ads.php returns an ad with an id, this example uses ad #10
// We are reading it with a full url because we don't want to read it as just
// a text file (we want it to be interpreted by php before we load it)
$content = file_get_contents('http://www.example.com/ads.php?id=10');
$line_height = 12; // line-height: 12px; is set in CSS
$number_of_lines = substr_count($content,'<br />'); // you can search for <br>
                                                    // and <br/> as well if you 
                                                    // need to
$height = $line_height * $number_of_lines; // You may also want to have padding

?>
<iframe src="ads.php?id=10" height="<?php echo $height; ?>" />

如果广告只是文本,可以使用
行高*行数
作为高度。我假设您知道字体大小,通过计算

的行数可以找到行数:

<?php
// $content is your ads
// assuming ads.php returns an ad with an id, this example uses ad #10
// We are reading it with a full url because we don't want to read it as just
// a text file (we want it to be interpreted by php before we load it)
$content = file_get_contents('http://www.example.com/ads.php?id=10');
$line_height = 12; // line-height: 12px; is set in CSS
$number_of_lines = substr_count($content,'<br />'); // you can search for <br>
                                                    // and <br/> as well if you 
                                                    // need to
$height = $line_height * $number_of_lines; // You may also want to have padding

?>
<iframe src="ads.php?id=10" height="<?php echo $height; ?>" />

您必须在javascript中设置iframe的高度。JavaScript应该位于iframe中的页面中(如果带有iframe的页面和iframe中的页面来自不同的域,则必须是)

a、 html:

<iframe src="b.html" id="myiframe"></iframe>

b、 html:

<div style="height: 500px; background:magenta;"></div>
<script>
    parent.document.getElementById('myiframe').style.height = document.body.offsetHeight+'px';
</script>

parent.document.getElementById('myiframe').style.height=document.body.offsetHeight+'px';

您必须在javascript中设置iframe的高度。JavaScript应该位于iframe中的页面中(如果带有iframe的页面和iframe中的页面来自不同的域,则必须是)

a、 html:

<iframe src="b.html" id="myiframe"></iframe>

b、 html:

<div style="height: 500px; background:magenta;"></div>
<script>
    parent.document.getElementById('myiframe').style.height = document.body.offsetHeight+'px';
</script>

parent.document.getElementById('myiframe').style.height=document.body.offsetHeight+'px';

如果只是相同高度的图像,这也会容易得多:$height=$image\u height*$number\u of\u image;哦,你可以用substr_count($content)计算图像,如果只是相同高度的图像,这也会容易得多:$height=$image_height*$number_of_images;哦,你可以用substr_count($content,'