【问题标题】:Adjust height of div to content将div的高度调整为内容
【发布时间】:2009-10-26 18:17:06
【问题描述】:

我有两个 div,一个在左侧,一个在页面右侧。 我的网站中间有一个 iframe,它的内容是从一个带有 mysql 结果的 php 文件加载的。 我希望页面上的两个 div 获取页面的总高度并将其设置为自己的高度,以便边框一直向下。我还希望 iframe 根据“myPhpFile”回显的“results_table”调整其高度。

这是我的两个 div 代码:

   <div class="bgr_left">
   <div class="bgr_right">

这是css:

  .bgr_right {
background-image: url(../Graphics/bgr_right.jpg);
background-repeat: repeat-y;
background-position: right;
position: absolute;
top: 0px;
width: 30px;
right: 0px;
background-color: #E7F5F0;
    }

   .bgr_left {
background-image: url(../Graphics/bgr_left.jpg);
background-repeat: repeat-y;
background-position: left;
position: absolute;
left: 0px;
top: 0px;
width: 30px;
background-color: #E7F5F0;
    }

这是表格内的 iframe:

    <table width="850px" height="1000px" border="0" align="center">
    <tr>
    <td width="845px" height="998px">
      <iframe style="border:none; width:100%; height:100%;" name="iframe001"  src="frame.html" id="iframe001"></iframe>
    </td>
    </tr>
    </table>

这是表格:

     <form id="nav_form_main" name="nav_form_main" action="bincgi/myPhp.php" target="iframe001" method="get">

这是 php 文件中从 mysql 查询创建表的部分:

      while($row = mysql_fetch_array($qry_result)){

我的问题:这种方式是在我的页面上获得两侧边框的最佳方式吗?而且,有没有办法将 iframe 高度设置为即将从我的“FORM”加载到其中的内容...

背景历史: 我的网站上有一个针对 iframe 的表单,表单上的操作是一个 php 文件,它在表中返回一个 mysql 查询结果。所以这张表最终在 iframe 中!

【问题讨论】:

  • 我从未使用过&lt;iframe&gt;,但在您的 div 上设置height: 100% 应该可以解决边框问题。
  • 如果您可以链接到示例页面,这将是一个很大的帮助。我和其他人会更容易为您调查此问题。
  • 抱歉还没上线……但我知道你的意思

标签: php javascript html css


【解决方案1】:

可以尝试使用 Javascript 调整 IFrame onload 的大小,但根据我的经验,这非常麻烦。使用 Ajax 将 PHP 结果动态加载到 HTML 结构中会更容易、更可靠。如果将 PHP 输出加载到 DIV 中,该 DIV 的高度会增加页面的高度,从而增加其他 DIV 的高度。

【讨论】:

    【解决方案2】:

    如果 iframe 和 JavaScript 在同一个域中,您可以使用 JavaScript 访问 iframe 内容高度。

    您可以尝试使用 PHP 插入内容,例如:

    $content = file_get_contents('http://domain.net/iframe.php');
    
    //
    //Do something with the content here
    //
    
    echo($content);
    

    但如果您不能修改iframe.php 以仅获取您需要的内容,则尚不清楚解决方案。我认为你最终会得到静态 iframe 高度。

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      相关资源
      最近更新 更多