我有以下脚本运行 Iframe 的 onload
<script type="text/javascript">
function getDocHeight(doc) {
doc = doc || document;
var body = doc.body, html = doc.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
return height;
}
function setIframeHeight(id) {
var ifrm = document.getElementById(id);
var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
ifrm.style.visibility = 'hidden';
ifrm.style.height = "10px"; // reset to minimal height in case going from longer to shorter doc
ifrm.style.height = getDocHeight( doc ) + "px";
ifrm.style.visibility = 'visible';
}
</script>
但是,在 Iframe 中,有一个切换选项 (Jquery toggle()),它可以更改 Iframe 的大小。
每当用户单击切换按钮时,如何启动上面的脚本?
最佳答案
调用parent.setIframeHeight()
在切换回调中,使用正确的 iframe 引用(可能使用 parent.getElementsByTagName('iframe')[0]
)
关于javascript - 切换后设置 Iframe 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39012200/