a.js
document.write("<iframe id='floatDiv' style='width:112px;padding-left:0px;' src='http://localhost:4905/Web/index.aspx?kfID="+kfID+"&style="+style+"&keyword="+keyword+"'"+" frameBorder='0' width='100%' height='229px' scrolling='no' ></iframe>");如何让iframe自适应index.aspx的高度?
onload="this.height=this.document.body.clientHeight"
你可以在B页面中加一个方法,
使B在加载完成后,执行这个方法:得到B的高度,通知parent也就是A,
执行A中的JS,完成F的高度变化.
在其他网站的页面上,也就是别人的网页来调用我的a.js,不知这是不是跨域来操作
if(top.location != self.location){
var frames = window.parent.document.getElementsByTagName('iframe');
for (var i=0;i<frames.length;i++){
if (frames[i].name == self.name){
frames[i].height = document.body.scrollHeight;
frames[i].width = document.body.scrollWidth;
if(frames[i].name == "content"){
if(frames[i].height<300)frames[i].height=300;
}
break;
}
}
}
}
resizeIFrameHeight();将这个代码放在作为iframe的页面的最后,不要放在head部分,不存在跨不跨域的问题,在IE和FF中工作正常,没有试过其他的浏览器
解决问题办法:index.aspx加
<body onload="parent.document.getElementById('floatDiv').height=document.getElementById('div_kf_Float').scrollHeight;;">
我这种情况并不适合所有情形,我的情形是index.aspx最外层'div_kf_Float'层,而且它的高试也是随里面的内容不同高低将不同。scrollHeight这个属性很多解决此问题
或者
width:expression(document.documentElement.clientWidth>600?"600":auto)