網(wǎng)上關(guān)于iframe高度自適應(yīng)的代碼有很多,但比較雜亂,有一些過(guò)于復(fù)雜,有一些存在明顯錯(cuò)誤。緣由心生給出了一段修改后的精簡(jiǎn)版本,感覺(jué)不錯(cuò),簡(jiǎn)單修正了其中一處opera下的錯(cuò)誤,代碼如下:
javascript部分:
<script type="text/javascript">
function setcwinheight(iframeobj){
if (document.getelementbyid){
if (iframeobj){
if (iframeobj.contentdocument && iframeobj.contentdocument.body.offsetheight){
iframeobj.height = iframeobj.contentdocument.body.offsetheight;
} else if (document.frames[iframeobj.name].document && document.frames[iframeobj.name].document.body.scrollheight){ iframeobj.height = document.frames[iframeobj.name].document.body.scrollheight;
}
}
}
}
</script>
html部分:
<iframe name="framecontent" src="*" frameborder="0" width="100%" onload="setcwinheight(this)"></iframe>
標(biāo)記*號(hào)的地方填入iframe頁(yè)面的地址。需要注意的是,src地址必須是在同一網(wǎng)站下,否則會(huì)出現(xiàn)“權(quán)限被禁止”的錯(cuò)誤。
新聞熱點(diǎn)
疑難解答