我正在根据接受 UI 输入的 SQL 查询结果加载 iframe
的内容。因此,需要定期更改 iframe 的高度,以使页面格式保持不变。
当页面最初加载时,我使用以下代码来确定 iframe
的高度:
onload="this.height=this.contentWindow.document.body.scrollHeight;"
这在页面第一次加载时有效;但是当用户动态更改iframe
的内容时,不会重新触发onload,并且iframe
保留其原始高度。
这就是页面初始加载时 iframe
在我的 index.php 中的呈现方式:
<script>document.write('<iframe id="mainpage-iframe" onload="this.height=this.contentWindow.document.body.scrollHeight;" src="page-body.php"></iframe>');</script>
当用户从菜单中做出选择以根据 SQL 查询结果更新 iframe
时,会触发如下所示的函数:
document.getElementById('mainpage-iframe').contentDocument.location.assign('page-body.php?selectvenue=' + venue + '&selectcity=' + city);
...将参数发送到 page-body.php。
所以,我的问题是:是否可以以某种方式“附加”到此结果,以便 document.write
函数包含 onload
操作?
最佳答案
使用 JQuery 更新调整大小时的高度
$(document).ready(function() {
$(window).resize(function() {
//Update height here
}
}
关于javascript - 将字符串添加到 iframe 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53745140/