父页根据iframe子页内容调整iframe页面大小

一.带有iframe的父页

1)html

<!DOCTYPE html>
<html>
<head>
 <title>带有iframe的父页</title>
</head>
<body>
    <iframe src="" id="businessPage" width="99%" height="150px;" style="margin:5px;" onclick="alert('aaa');"></iframe>
</body>
</html>

2)js

$(document).ready(function(){
    $("#businessPage").load(function(){//用于每次刷新时控制iframe高度初始化
 	    //$(this).height(0); 
 	    var iframeHeight;
 	    if($(this)[0].contentWindow.getApproveBusinessPageHeight){
 	        iframeHeight = $(this)[0].contentWindow.getApproveBusinessPageHeight();
 	    }else{
 	     	iframeHeight = $(this).contents().height() + 40;
 	    }
 	    $(this).height( iframeHeight < 150 ? 150 : iframeHeight );
    });
 	$("#businessPage").attr("src", "${business.pageUrl }");
}); 

二.iframe加载的子页

1)js

/*自定义iframe 高度的方法*/
function getApproveBusinessPageHeight(){
    return $("#comMenuFrame").height() + 40;
}

猜你喜欢

转载自blog.csdn.net/xm393392625/article/details/84578314