JQuery IFrame框架高度自适应(支持嵌套–兼容IE,ff,safafi,chrome)

在网上找了很多的iframe自适应高度的脚本,找到了比较好的

jquery很强大,代码很简单:

$("#iframe_1").load(function() { 

$(this).height($(this).contents().height()); 

}) 


Index.hmtl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<div style="height:200px"></div>
<iframe id="iframe_1" src="iframe-inside-content.html" width="100%" border="2" scrolling="no"></iframe>
<script type="text/javascript">
$("#iframe_1").load(function() {
$(this).height($(this).contents().height());
})
</script>
</body>
</html>

iframe-inside-content.html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
function hideDiv(){
 $("#content").hide();
 $(window.parent.document).find('#iframe_1').height($("#wrap").height());
}

function showDiv(){
 $("#content").show();
 $(window.parent.document).find('#iframe_1').height($("#wrap").height());
}
</script>
<style>
*{margin:0;paddding:0;}
body{background:transparent;background-color:#eee;height:100%;}
#content{height:1000px;background-color:#ddd;margin:50px;}
</style>
</head>
<body>
 <div id="wrap">
 <a href="#" οnclick="hideDiv();">隐藏DIV</a>
 <a href="#" οnclick="showDiv();">显示DIV</a>
 <div id="content">div content</div>

 <div>div2222 content</div>
 </div>

 </div>
</body>
</html>

开始找到代码的时候直接下载到本地去测试,发现谷歌浏览器是不行的。 protocols and ports must match. 这种错误,于是查了下,发现貌似是跨域问题,把他们拿到服务器上 用地址去访问就可以了,也就是说谷歌浏览器对file:///C:/Users/xx.xx/Desktop/index.html 认为file:///C:/Users/xx.xx/Desktop/iframe-inside-content.html 不是同域的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值