1
function calcPageHeight(doc) { var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight) var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight) var height = Math.max(cHeight, sHeight) var height1 = height+430 return height1 } //根据ID获取iframe对象 var ifr = document.getElementById('mainFrame') ifr.onload = function() { //解决打开高度太高的页面后再打开高度较小页面滚动条不收缩,第一步置为绝对不能少,否则后面会一直保持一个高度 ifr.style.height='0px'; var iDoc = ifr.contentDocument || ifr.document var height = calcPageHeight(iDoc) ifr.style.height = height + 'px' }
2
//函数形式
function iframeH(){ //设置iframe高度 parent.document.getElementById("mainFrame").height=0+"px"; parent.document.getElementById("mainFrame").style.height = ""; var h = $(document.body).height() + 120; parent.document.getElementById("mainFrame").height = h; }
//选择元素形式
$(window.parent.document).find("#mainFrame").load(function () {
if($(document).height()!=null&&$(document).height()!='0'){
parent.document.getElementById("mainFrame").height=0+"px";
parent.document.getElementById("mainFrame").style.height = "";
parent.document.getElementById("mainFrame").height = $(document).height();
}
});