我有一个页面,我在其中插入了一个 iframe。加载子页面时,我使用以下代码动态设置 iframe 的值:
父页面:
`<iframe src="/test.do" id="iframeTest">`
function setIframeHeight(height) {
$("#iframeTest").height(height);
}
子页面:test.jsp:
<div id="test">
<table>The whole content will be inside this div</table>
</div>
$(document).ready(function() {
var height = $("#test").outerHeight();
parent.setIframeHeight(height);
}
现在,每当子页面的高度增加时(即每当包含新的 <tr>
时),我将调用 parent.setIframeHeight()
这样iframe的高度就增加了。我对此进行了测试,对我来说效果很好。我想知道增加 iframe 高度是否正确?或者有没有其他选择。
最佳答案
试试这个: 如果您的子页面内容连续更改或在一段时间后更改:
setInterval(function(){iframeLoaded}, 2000);
function iframeLoaded() {
var iFrameID = document.getElementById('iframeTest');
if(iFrameID) {
// here you can make the height, I delete it first, then I make it again
iFrameID.height = "";
iFrameID.height = iFrameID.contentWindow.document.body.scrollHeight + "px";
}
}
关于javascript - 动态增加iframe的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23947868/