iframe height 100% 无效问题解决(转)

最近,利用 MapGuide 技术开发一个 WebGIS 的应用程序,其中用到了 <iframe> 标签;可是当我调试运行的时候,其 width=100% 生效了,但 height=100% 就无效,无论用 JavaScript 的方式修改还是直接设置其 height 属性为100%,始终只有 200px 左右的高度。折腾了我半天,再经过一番研究,终于找到答案了,结论如下:要使 <iframe> 标签的 height=100% 生效,一定要保证其父容器的 height=100% 有效(但我仍然想不通的是,为什么 width=100% 就没问题呢?)。现在举例如下:

 

在 index.html 中的代码如下:

 

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02 <html xmlns="http://www.w3.org/1999/xhtml">
03 <head>
04     <title>iframe tag test</title>
05     <style type="text/css">
06         html, body
07         {
08             margin: 0px 0px;
09             width: 100%;
10             height: 100%;
11         }
12         iframe
13         {
14             margin: 0px 0px;
15             width: 100%;
16             height: 100%;
17         }
18     </style>
19  
20     <script type="text/javascript">
21         function iframeHeight() {
22             document.getElementById('iframeId').height="100%";
23         }
24     </script>
25  
26 </head>
27 <body>
28     <iframe id="iframeId" frameborder=0 scrolling=no
29         src=http://www.google.com />
30 </body>
31 </html>

 

在上述代码中,样式代码部分明确指出了 html, body 的 width 和 height 为 100%,这是必须的,随后指出了 iframe 的 width 和 height 也为 100%,这里的意思是说 iframe 的 width 和 height 是相对于其父容器的 width 和 height 为 100%,这样的方法既简单又明了,IE、Firefox(火狐浏览器)、chrome(Google浏览器)均能顺利通过。希望遇到此问题的同志不要再 走弯路!


转自:http://blog.baiwand.com/?post=3

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值