其他分享
首页 > 其他分享> > iframe高度自适应

iframe高度自适应

作者:互联网

<iframe src="./page/header.html" id="iframepage"  width="100%" height="auto"  frameborder="0" scrolling="no" marginheight="0" onl oad="iFrameHeight()"></iframe>

onLoad="iFrameHeight()"必要  调用iFrameHeight()事件  id需要在事件中使用

// 头部高度自适应
function iFrameHeight() {
			var ifm= document.getElementById("iframepage");
			var subWeb = document.frames ? document.frames["iframepage"].document : ifm.contentDocument;
			if(ifm != null && subWeb != null) {
				ifm.height = subWeb.body.scrollHeight;
				ifm.width = subWeb.body.scrollWidth;
			}
		}

创建js,将上面代码复制进去,在嵌套iframe标签的html文件中调用js

若一个页面需要嵌套多个iframe,则定义不同的id名,将

var ifm= document.getElementById("iframepage");
			var subWeb = document.frames ? document.frames["iframepage"].document : ifm.contentDocument;
			if(ifm != null && subWeb != null) {
				ifm.height = subWeb.body.scrollHeight;
				ifm.width = subWeb.body.scrollWidth;
			}

这一块复制即便,修改获取的id值即可

 引入js放在最底部,放在头部可能会出去获取不到dom的情况

如果报“id”undefined或者default,是因为此页面没有这个dom节点,可以选择多封装几个事件,不同iframe调用不同onlode事件

标签:body,iframepage,高度,适应,iframe,frames,ifm,subWeb,document
来源: https://blog.csdn.net/love_weiai/article/details/121212728