获取iframe的内容高度

const iframe = document.getElementById('iframeId');
iframe.onload =function(){
try{
  const doc = iframe.contentDocument||iframe.contentWindow.document;
  const body = doc.body;
  const html = doc.documentElemet;
  const height = Math.max(
              body.scrollHeight ,
              body.offsetHeight,
              html.scrollHeight,
              html.offsetHeight  
)        ;
iframe.style.height=height+'px';


}.catch(e){
console.log('无法获取iframe内容高度',e)
}
}

 

posted @ 2025-11-21 10:41  木樨园  阅读(5)  评论(0)    收藏  举报