如何在包含页面内调整iframe的大小?

poo*_*poo 5 javascript iframe

我想访问我的页面所在的iframe并调整其大小.顺便说一句,我在javascript中这样做.

它类似于父级和高度或者偏移高度.

<iframe src="mypage.asp" height="400" width="400" ></iframe> 
Run Code Online (Sandbox Code Playgroud)

并在mypage.asp我做类似这样的事情:

var h = // new height;
parent.height = h; 
Run Code Online (Sandbox Code Playgroud)

但这不是很好吗?还有谁知道更多?

npu*_*pup 6

如果您想从 iframe 中加载的页面内调整 iframe 的大小,请尝试此操作。它似乎至少在本地工作:

function doIt() {
    var elem = window.parent.document.getElementById('myIframe'); // the id of your iframe of course
    elem.style.height = '40em';
}
Run Code Online (Sandbox Code Playgroud)

我假设页面和您的 iframe 都是您的并且具有相同的“来源”。

  • 当使用跨域页面时,这会严重失败。 (2认同)