iframe 标签自适应高度和宽度

iframe 结构如下

<iframe src="index.html" id="frame" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" onLoad="iFrameHeight()"></iframe>

js 实现

<script type="text/javascript" language="javascript">
function iFrameHeight() {
var ifm = document.getElementById("frame");
var subWeb = document.frames ? document.frames["frame"].document : ifm.contentDocument;
if (ifm != null && subWeb != null) {
ifm.height = subWeb.body.scrollHeight;
ifm.width = subWeb.body.scrollWidth;
}
}
</script>

jQuery 实现

$("#frame").load(function(){
var mainheight = $(this).contents().find("body").height()+30;
$(this).height(mainheight);
});
上一篇:步步为营-66-Socket通信


下一篇:关于使用iframe标签自适应高度的使用