<iframe src="需要连接的iframe地址" id="iframepage" name="iframepage" frameBorder=0 scrolling=no width="100%" onLoad="iFrameHeight()" >
</iframe>
js代码:function setIFrameHeight(iframe) {
var oHeight = 0;
var sHeight = 0;
var cHeight = 0;
if (iframe && !window.opera) {
if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) {
oHeight = iframe.contentDocument.body.offsetHeight;
}
if (iframe.Document && iframe.Document.body.scrollHeight) {
sHeight = iframe.Document.body.scrollHeight;
}
if(iframe.contentDocument && iframe.contentDocument.documentElement.scrollHeight){
cHeight = iframe.contentDocument.documentElement.scrollHeight;
}
var lheight = Math.max(oHeight, sHeight);
var height = Math.max(lheight, cHeight);
iframe.height = height + 20;
}
}
</iframe>
js代码:function setIFrameHeight(iframe) {
var oHeight = 0;
var sHeight = 0;
var cHeight = 0;
if (iframe && !window.opera) {
if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) {
oHeight = iframe.contentDocument.body.offsetHeight;
}
if (iframe.Document && iframe.Document.body.scrollHeight) {
sHeight = iframe.Document.body.scrollHeight;
}
if(iframe.contentDocument && iframe.contentDocument.documentElement.scrollHeight){
cHeight = iframe.contentDocument.documentElement.scrollHeight;
}
var lheight = Math.max(oHeight, sHeight);
var height = Math.max(lheight, cHeight);
iframe.height = height + 20;
}
}
本文介绍了一种使用JavaScript实现iframe自适应高度的方法。通过监听iframe内容的变化并调整iframe的高度,确保其始终能够完美适配内容高度,避免多余滚动条出现。适用于网页开发中iframe元素的优化。
2207

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



