这是我的问题。我一直在努力寻找一个跨浏览器的iframe大小调整代码,但我就是找不到。我所看到的只是一个浏览器中的问题。这是我正在做的事情。我正在将一个iframe加载到jquery工具覆盖的页面中。这个iframe将加载页面的内容(在同一个域上,所以不需要担心跨域)。当用户单击该表单上的某个操作时,iframe将再次需要调整大小(我让它在iframe增加时工作,但在iframes减少时不工作)。
我有一个js文件,包含在iframe中,它具有功能
$(window).load(function(){
parent.adjust_iframe();
});
然后,该函数调用父页面函数,如下所示:
function adjust_iframe() {
//i have tried both body and html and both dont work in IE
var h = $("#overlayFrame").contents().find("body").height();
if(h==0)
h="500";
else
h=h+3;
$("#overlayFrame").css({'height': h});
window.scrollTo(0,0);
}
上面的代码在Chrome和firefox中运行良好,但在IE中则不然
这里有人帮忙吗?我真的需要一个跨浏览器兼容的轻量级解决方案,它不涉及一些不受支持的重型jquery插件。
谢谢!
尝试
$(window).load(function(){
var bodyHeight = $('body').height();
parent.adjust_iframe( bodyHeight );
});
和
function adjust_iframe(newHeight) {
//i have tried both body and html and both dont work in IE
if(newHeight == 0) {
newHeight = 500;
} else {
newHeight += 3;
}
$("#overlayFrame").css({'height': newHeight});
window.scrollTo(0,0);
}
因为问题可能是页面无法访问iframes的内容。。
我有两个建议:
当你设置CSS的高度,明确告诉它像素。
$("#overlayFrame").css({'height': h + 'px'});
当iframe代码调用parent.adjust_iframe时,发送当前宽度/高度。
parent.adjust_iframe($('body').height());
奖金建议:做一点调查,告诉我们IE的版本以及为什么它不起作用。在那里放一些警报,看看高度是否派生等。
我搜索了存档的文件,找到了设置iframe窗口新大小的脚本。它正在处理IE6、FF,。。。
/**
* Parent
*/
<iframe id="myframe" name="myframe" ...>
<script type="text/javascript">
var iframeids=["myframe"];
if (window.addEventListener) {
window.addEventListener("load", resizeCaller, false);
}else if (window.attachEvent) {
window.attachEvent("onload", resizeCaller);
} else {
window.onload=resizeCaller;
}
var iframehide="yes";
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1];
var FFextraHeight=parseFloat(getFFVersion)>=0.1? 20 : 0;
function resizeCaller() {
var dyniframe=new Array();
for (i=0; i<iframeids.length; i++){
if (document.getElementById)
resizeIframe(iframeids[i]);
if ((document.all || document.getElementById) && iframehide=="no"){
var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i]);
tempobj.style.display="";
}
}
};
function resizeIframe(frameid){
var currentfr=document.getElementById(frameid);
if (currentfr && !window.opera){
currentfr.style.display="";
if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight)
currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight;
else if (currentfr.Document && currentfr.Document.body.scrollHeight)
currentfr.height = currentfr.Document.body.scrollHeight;
if (currentfr.addEventListener)
currentfr.addEventListener("load", readjustIframe, false);
else if (currentfr.attachEvent){
currentfr.detachEvent("onload", readjustIframe);
currentfr.attachEvent("onload", readjustIframe);
}
}
};
function readjustIframe(loadevt) {
var crossevt=(window.event)? event : loadevt;
var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement;
if (iframeroot)resizeIframe(iframeroot.id);
};
function loadintoIframe(iframeid, url){
if (document.getElementById)document.getElementById(iframeid).src=url;
};
</script>
/**
* child iFrame html
*/
<body onResize="resizeIE()">