---
title: "iframe自适应高度"
author: "wangning8075"
date: "2015-06-26T01:02:03Z"
tags: []
categories: ["Web前端"]
slug: "iframe"
draft: false
lastmod: "2016-11-09T21:18:10Z"
id: 2222172
url: "https://wangning8075.iteye.com/blog/2222172"
---

使iframe自适应高度

两点注意：

1）js代码放在iframe标签下

2）相应的需要在iframe标签中展现的页面要有高度

 

```
<iframe id="iframe" name="iframe"  style="border:0px;width:740px;height:415px;" src="<?php echo site_url('index/home/index_main')?>"
              frameborder="0" width="100%" scrolling="no" height="100%"></iframe>
            <script type="text/javascript">
            //注意：下面的代码是放在和iframe同一个页面调用,放在iframe下面
           $("#iframe").load(function () {
           var mainheight = $(this).contents().find("body").height() + 30;
            $(this).height(mainheight);
               });
            </script>
```