<script>
/***********************监听iframe的onclik事件*****************/
var timeHandle;
function setEvent(){
try{
window.frames["childframe"].document.body.onclick=function(){
var f=document.getElementById('childframe');
f.style.height = f.contentWindow.document.body.scrollHeight + "px";
}
}catch(e){}
timeHandle=setTimeout(setEvent,200);
}
setEvent();
/*************************装载的时候自适应高度************************/
function resizeFrame(f) {
f.style.height = f.contentWindow.document.body.scrollHeight + "px";
}
</script>
<iframe id=childframe onload="resizeFrame(document.getElementById('childframe'))" scrolling="no" name=childframe src="yoursrc" width=100% height=100% frameborder=0 border=0 ></iframe>
/***********************监听iframe的onclik事件*****************/
var timeHandle;
function setEvent(){
try{
window.frames["childframe"].document.body.onclick=function(){
var f=document.getElementById('childframe');
f.style.height = f.contentWindow.document.body.scrollHeight + "px";
}
}catch(e){}
timeHandle=setTimeout(setEvent,200);
}
setEvent();
/*************************装载的时候自适应高度************************/
function resizeFrame(f) {
f.style.height = f.contentWindow.document.body.scrollHeight + "px";
}
</script>
<iframe id=childframe onload="resizeFrame(document.getElementById('childframe'))" scrolling="no" name=childframe src="yoursrc" width=100% height=100% frameborder=0 border=0 ></iframe>
本文介绍了一种通过JavaScript实现iframe自适应高度的方法。该方法通过监听iframe内的body元素点击事件及在加载完成后调整iframe的高度,使其能够自动匹配内容的实际高度,避免了不必要的滚动条出现。
2558

被折叠的 条评论
为什么被折叠?



