纠结了很久的iframe自适应
在网上看了很多的iframe自适应,使用了以后也没见效。最后发现问题竟然出现在iframe的style设置上。设置iframe的宽高时不能用style="width:100px;height:100px",需要直接写width="700" height="200"。不知道什么原因,但这样的确是有效解决了我的问题。
iframe:
iframe:
<iframe src="dir.htm" name="iframename" id="iframename" width="700" align="center" height="200"
onload="Javascript:SetWinHeight(this)" frameborder="0" scrolling="no"></iframe>
js:
function SetWinHeight(obj)
{
var win=obj;
if (document.getElementById)
{
if (win && !window.opera)
{
if (win.contentDocument && win.contentDocument.body.offsetHeight)
win.height = win.contentDocument.body.offsetHeight;
else if(win.Document && win.Document.body.scrollHeight)
win.height = win.Document.body.scrollHeight;
}
}
}