动态调整 iframe 的高度
我有一个 iframe,其中包含网站的一些内容。我希望 iframe 调整为 src 内容高度的 100%。 js 方面的菜鸟 - 这就是我正在处理的内容:
<iframe id="frame" scrolling="no" frameborder="0" src="http://www.srcwebsite.org"></iframe>
<script type="text/javascript">
function resizeIframe() {
var height = document.documentElement.clientHeight;
height -= document.getElementById('frame').offsetTop;
height -= 20; /* whatever you set your body bottom margin/padding to be */
document.getElementById('frame').style.height = height +"px";
};
document.getElementById('frame').onload = resizeIframe;
window.onresize = resizeIframe;
</script>
这对于调整 iframe 的高度到其页面的高度非常有用,但我希望它将其调整到 src 的高度......
I have an iframe that contains some content from a site. I want the iframe to adjust to 100% of the src content's height. Bit of a noob at js - here's what I'm working with:
<iframe id="frame" scrolling="no" frameborder="0" src="http://www.srcwebsite.org"></iframe>
<script type="text/javascript">
function resizeIframe() {
var height = document.documentElement.clientHeight;
height -= document.getElementById('frame').offsetTop;
height -= 20; /* whatever you set your body bottom margin/padding to be */
document.getElementById('frame').style.height = height +"px";
};
document.getElementById('frame').onload = resizeIframe;
window.onresize = resizeIframe;
</script>
This works great to resize the height of the iframe to its page, but I want it to resize it to the height of the src...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于同源政策,您无法访问
iframe
内容的高度(协议、域和端口必须匹配)。您可以设置 CORS,请记住,在 IE8 之前,IE 不支持它。
You can't access the height of the
iframe
's content because of Same Origin Policy (protocol, domain and ports must match).You could set up CORS, keep in mind it's not supported in IE until IE8.