最新做的項目採用的iframe,但是在使用bootstrapTable這類高度動態變化時,iframe的高度要麼太高要麼太低導致頁面被遮擋,而且頁面也不美觀。網上也找了很多方案但是結果都是不太理想,結合其他人的經驗總結下自己的這個方法。
//動態判定iframe頁面高度
function setIframeHeight() {
var iframe = document.getElementById('mainFrame');// mainFrame爲當前的iframe的id
if (iframe) {
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
if (iframeWin.document.body) {
iframe.height = iframeWin.document.body.scrollHeight || iframeWin.document.documentElement.scrollHeight;
}
}
};
window.onload = function () {
setIframeHeight();
};
window.setInterval("setIframeHeight()", 200);