1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > js 控制iframe 自适应高度

js 控制iframe 自适应高度

时间:2021-08-23 16:17:50

相关推荐

js 控制iframe 自适应高度

<iframe width="300" id="iframetestid" frameborder="0" scrolling="no" src="test.php" οnlοad="setTimeout(function(){reSetIframe('iframetestid');},200);" ></iframe>

function reSetIframe(iframeid){var iframe = document.getElementById(iframeid);var height = 500;try{var bHeight = iframe.contentWindow.document.body.scrollHeight;var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;height = Math.max(bHeight, dHeight);iframe.height = height;}catch (ex){height = Math.max(bHeight, dHeight);iframe.height = height;}}

这个适应于各种浏览器,从ie6 7 8 9 一直到 ff,chrome

调试了很久的ie6一直取不到值,最后发现原因是要设置函数reSetIframe定时执行,直接执行虽然用onload还是取不到,要延迟一会ie6才能得到height值。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。