Synchronize the height of iframe and parent

本文介绍了一种通过JavaScript调整iframe元素高度的方法,确保iframe能够自适应其内容高度,避免出现滚动条的同时保持良好的页面布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Just a test!

Tips:
o.height = ifr.h;
can not be changed with:
o.height = o.h;

Parent[iframe.html]
  1. <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <title> New Document </title>
  5. <style>
  6. body {
  7.     padding: 0px;
  8.     margin: 0px;
  9. }
  10. </style>
  11. <script>
  12. var h;
  13. </script>
  14. </head>
  15. <body onload="test()">
  16. <table border="1">
  17. <tr>
  18. <td>There is a test</td>
  19. </tr>
  20. <tr>
  21. <td><iframe id="ifr" src="content.html" width="100%" frameborder="0" scrolling="no"></iframe></td>
  22. </tr>
  23. </table>
  24. </body>
  25. <script>
  26. function test() {
  27.     var o = document.getElementById("ifr");
  28.     o.height = ifr.h;
  29. }
  30. </script>
  31. </html>
Iframe[content.html]
  1. test<br/>
  2. test<br/>
  3. test<br/>
  4. test<br/>
  5. test<br/>
  6. test<br/>
  7. test<br/>
  8. test<br/>
  9. test<br/>
  10. test<br/>
  11. test<br/>
  12. test<br/>
  13. test<br/>
  14. test<br/>
  15. test<br/>
  16. test<br/>
  17. test<br/>
  18. test<br/>
  19. test<br/>
  20. test<br/>
  21. test<br/>
  22. test<br/>
  23. test<br/>
  24. test<br/>
  25. test<br/>
  26. test<br/>
  27. test<br/>
  28. test<br/>
  29. test<br/>
  30. test<br/>
  31. test<br/>
  32. test<br/>
  33. test<br/>
  34. test<br/>
  35. test<br/>
  36. test<br/>
  37. test<br/>
  38. test<br/>
  39. test<br/>
  40. test<br/>
  41. test<br/>
  42. test<br/>
  43. test<br/>
  44. test<br/>
  45. test<br/>
  46. test<br/>
  47. test<br/>
  48. test<br/>
  49. test<br/>
  50. test<br/>
  51. test<br/>
  52. test<br/>
  53. test<br/>
  54. test<br/>
  55. test<br/>
  56. test<br/>
  57. test<br/>
  58. test<br/>
  59. test<br/>
  60. test<br/>
  61. test<br/>
  62. test<br/>
  63. test<br/>
  64. test<br/>
  65. test<br/>
  66. test<br/>
  67. test<br/>
  68. test<br/>
  69. test<br/>
  70. test<br/>
  71. test<br/>
  72. test<br/>
  73. test<br/>
  74. test<br/>
  75. test<br/>
  76. test<br/>
  77. test<br/>
  78. test<br/>
  79. test<br/>
  80. End<br/>
  81. <script>
  82. var h;
  83. window.onload = function() {
  84.     h = document.body.scrollHeight;
  85. }
  86. </script>

====================================================================================
a simple method to realize it.

function test() {
    var o = document.getElementById("ifr");
    o.height = o.Document.body.scrollHeight;
}

window.onload = test;

caution: the "o.Document" not "o.document".
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值