gpt4 book ai didi

javascript - 单击 iframe 中的某些链接时如何更新 iframe 端?

转载 作者:行者123 更新时间:2023-11-28 00:50:50 24 4
gpt4 key购买 nike

我有以下代码,它根据 IFrame 内容的大小调整 IFrame 的大小:

     <html>
<head>
<title></title>
</head>
<body>
<iframe src="http://page.html"
id="iframeid" width="600" height="700" scrolling="no" onload="setIframeHeight(this.id)"></iframe>
<script>
function setIframeHeight(id) {
var ifrm = document.getElementById(id);
var doc = ifrm.contentDocument? ifrm.contentDocument:ifrm.contentWindow.document;
ifrm.style.height = "10px";
ifrm.style.height = getDocHeight(doc) + 4 +"px";
}

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;
}
$('iframe').css('height', $('iframe body').height() + 'px');
</script> </body> </html>

这很好用。 IFrame 大小根据导入网站的大小设置

http//:page.html

问题是当 http://:page.html 包含链接时。单击此链接时,iframe 大小不会更新。

问题是:点击链接时如何更新 iframesize?

最佳答案

为 iframe 中的加载添加事件监听器以触发您的函数:

$("iframe").on("load", function() {

setIframeHeight(id);

});

只要 iframe URL 与父文档的域相同,此操作就应该有效。如果不是,您可能会遇到浏览器安全限制,从而导致失败。

关于javascript - 单击 iframe 中的某些链接时如何更新 iframe 端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com