JavaScript如何设置Iframe高度
有时候在网页中可能需要嵌入Iframe,而对Iframe的控制又不能固定,那么就可以自动根据Iframe中内容进行自动设置高度。在HTML的Iframe标签中加入一个onload事件,就是在Iframe页面加载完毕时进行计算设置高度。
function SetIFrameHeight(iFrameId) {
if (iFrameId == "") {return;}
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : 0;
var pTar = null;
if (document.getElementById) {
pTar = document.getElementById(iFrameId);
} else {
eval('pTar=' + iFrameId + ';');
}
pTar.style.display = "block";
if (Sys.ie) {
if (Sys.ie == '9.0') {
pTar.height = pTar.contentWindow.document.body.offsetHeight + 15 + "px";
pTar.width = pTar.contentWindow.document.body.scrollWidth + "px";
} else if (Sys.ie == '8.0') {
pTar.height = pTar.Document.body.offsetHeight + 25 + "px";
pTar.width = pTar.Document.body.scrollWidth + "px";
} else {
pTar.height = pTar.Document.body.scrollHeight + 25 + "px";
pTar.width = pTar.Document.body.scrollWidth + "px";
}
}
}
参数就是该Iframe的id传入,暂时通过的是IE8测试是没问题的。
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64532.shtml