iframe 自适应高度解决

本文介绍了一种使用HTML和JavaScript实现的交互式统计图表应用,通过动态加载不同统计页面并实时更新iframe内容,展示部门、栏目、上稿及图标统计,同时详细解释了如何设置iframe高度以适应不同内容长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html:

	<body>

		<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
			<ul class="layui-tab-title">
				<li class="layui-this" data-value="/gcsoft/news/newsCountCharPage.action">图标统计</li>
				<li data-value="/gcsoft/news/countListPage.action">上稿统计</li>
				<li data-value="/gcsoft/news/newscountDeptListPage.action">部门上稿统计</li>
				<li data-value="/gcsoft/news/newscountMenutListPage.action">栏目上稿统计</li>
			</ul>
			<div><iframe id="iframe" name="iframe" frameborder="0" border="0" marginwidth="0" marginheight="0" scrolling="no" src="/gcsoft/news/newsCountCharPage.action" style="width:100%;magrin:0 auto"></iframe></div>
		</div>
	</body>

js:

 	   //点击刷新iframe
		layui.use(['element'], function() {});
		$(".layui-tab-title li").click(function() {
			var href = $(this).attr("data-value");
			$("#iframe").attr("src", href);
			$("#iframe").css("height", '');
			setIframeHeight();
			document.getElementById('iframe').src = href;
		});

         //设置iframe高度函数
		function setIframeHeight() {
			var iframe = document.getElementById('iframe');
			var height = window.parent.document.documentElement.scrollHeight - 165;//获取父页面高度减去差值
			if(document.all) { //判读ie w3c
				iframe.attachEvent("onload", function() { //ie 判断加载 
					var temph = iframe.contentWindow.document.documentElement.scrollHeight;
					iframe.height = temph > 400 ? temph : height;//给与一个界限
				});
			} else {
				var ifr = $("#ifr", parent.document);
				iframe.onload = function() { //w3c判断加载
					var temph = iframe.contentDocument.body.scrollHeight;
					iframe.height = temph > 400 ? temph : height;//给与一个界限
				}
			}
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值