jQuery,js实现Iframe自适应高度(兼容多浏览)

本文介绍两种使用jQuery实现自适应调整iframe高度的方法。第一种方法通过监听iframe加载完成事件,动态调整其高度;第二种方法直接利用iframe的内容高度进行调整。

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

// 我们在aa.html里面有个iframe,加载内容为bb.html


<iframe id="frame_con" src="bb.html" frameborder=0 scrolling=no style="margin-left:0px;margin-top:-3px;width:785px;height:550px;float:right;"></iframe>


[color=red]方式一[/color]

//引入jQuery
<script src="http://res.func.fetionpictest.com/js/public/jquery-1.3.2.min.js?2011122701" type="text/javascript"></script>
<script type="text/javascript">
//document.domain = "fx-func.com";//指向根域
$(window.parent.document).find("#frame_con").load(function(){//绑定事件
var main = $(window.parent.document).find("#frame_con");//找到iframe对象
var thisheight = $(document).height()+30;//获取页面高度
main.height(thisheight < 500 ? 500 : thisheight);
//为iframe高度赋值如果高度小于500,则等于500,反之不限高,自适应
});
</script>


[color=red]方式二[/color]

$("#frame_con").load(function(){
var thisheight = $(this).contents().find("body").height()+30;
$(this).height(thisheight < 500 ? 500 : thisheight);
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值