当前位置:Gxlcms > JavaScript > 兼容主流浏览器的iframe自适应高度js脚本_javascript技巧

兼容主流浏览器的iframe自适应高度js脚本_javascript技巧

时间:2021-07-01 10:21:17 帮助过:3人阅读

兼容主流浏览器的 Iframe 自适应高度,js脚本如下:
代码如下:

//iframe 高度自适应
function iframeAutoFit(iframeObj) {
setTimeout(function () {
if (!iframeObj) return;
iframeObj.height = (iframeObj.Document ? iframeObj.Document.body.scrollHeight : iframeObj.contentDocument.body.offsetHeight);
}, 200);
}

调用方法:
代码如下:



扩展:
代码如下:

//动态添加样式
function addSheetFile(obj, path) {
if (obj == undefined || obj == null)
obj = document;

var fileref = obj.createElement("link")
fileref.rel = "stylesheet";
fileref.type = "text/css";
fileref.href = path;
fileref.media = "screen";
var headobj = obj.getElementsByTagName('head')[0];
headobj.appendChild(fileref);
}

人气教程排行