请问如何自动设置iframe的高度

[code="javascript"]
$(".workPanel .tabs .tabs-content").append("

");

var iframe = document.getElementsByName("ifram")[0];
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
alert(bHeight+","+dHeight);
var height = Math.max(bHeight, dHeight);
iframe.height = height + 20;
}catch (ex){}
[/code]

请问我这么写有什么问题吗?

[code="java"]



[/code]

你应该 这样写 :在 iframe 添加一个 onload 事件 ,要等他加载完成之后 才注入 ifrrame的高度

[code="java"]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;
}
}
} [/code]

[code="java"] [/code]