在JS中先获取iframe元素,然后通过获取到的实例获取对应iframe页面的高度
var iframe = document.getElementById("xxx");//为iframe框架 id名
//iframe自适应高度
function setIframeHeight(iframe) {
if (iframe) {
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
if (iframeWin.document.body) {
iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
}
}
};
// 为了满足src灵活变换实现高度自适应,所以要在 iframe.onload中重新计算高度
iframe.onload = function () {
// 为了兼容IE, 根据iframe动态的SRC自适应高度
iframe.height = iframe.contentWindow.document.body.scrollHeight
}
个人理解,如果有什么问题可以提问