是否有可能iframe在没有主窗口帮助的情况下自行调整大小? [英] is it possible that iframe resize itself without help from main window?
问题描述
主窗口和iframe都位于同一个域中,但我想要实现的是调整iframe大小,以便仅在iframe中使用js来适应其内容。
Both main window and iframe are on the same domain, but what I want to achieve is to resize iframe to fit its contents using js only within that iframe.
我也不知道主窗口分配给它的iframe的id。而且我不想使用jquery或任何其他框架。
I also do not know the id of the iframe that is assigned to it by main window. And I do not want to use jquery or any other framework for it.
推荐答案
你也可以在不知道<$的情况下这样做父窗口中 iframe
的c $ c> id :
You can do this also without knowing the id
of the iframe
in the parent window:
window.frameElement.style.width = iframeContentWidth + 'px';
window.frameElement.style.height = iframeContentHeight + 'px';
在 frameElement https://developer.mozilla.org/en-US/docs/DOM/window.frameElement\"rel =noreferrer> MDN 。
See frameElement
at MDN.
编辑
以防万一 iframe
恰好位于已修复的容器元素中大小和溢出:隐藏
你可以这样做:
Just in case the iframe
happens to be in a container element which has fixed size and overflow: hidden
you can do something like this:
function resizeIframe (iframeContentWidth, iframeContentHeight) {
var container = window.frameElement.parentElement;
if (container != parent.document.body) {
container.style.width = iframeContentWidth + 'px';
container.style.height = iframeContentHeight + 'px';
}
window.frameElement.style.width = iframeContentWidth + 'px';
window.frameElement.style.height = iframeContentHeight + 'px';
return;
}
这篇关于是否有可能iframe在没有主窗口帮助的情况下自行调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!