今天小编给大家分享一下js如何控制Iframe高度自适应的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。创新互联公司专业提供成都网站设计、网站建设,网页设计,网站制作(企业站、响应式网站开发、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!
方法一:js控制Iframe 高度自适应
这个方法之前一直都在用,没有问题,但最新发现有些情况不行(具体原因不清楚)
/* function thisIframeHeightAuto(){ setIframeHeight("auditList"); }; */ //window.setInterval("iframeHeightAuto()", 200); function setIframeHeight(iframeId){ var cwin = document.getElementById(iframeId); if(document.getElementById){ if(cwin && !window.opera){ if(cwin.contentDocument && cwin.contentDocument.body.offsetHeight){ cwin.height = cwin.contentDocument.body.offsetHeight;//FF NS console.log("FF NS cwin.height=" +cwin.height); }else if(cwin.Document && cwin.Document.body.scrollHeight){ cwin.height = cwin.Document.body.scrollHeight;//IE console.log("IE cwin.height=" +cwin.height); } }else if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight){ cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera } } console.log("cwin.height=" + cwin.height); };
方法二:html代码控制
在方法一不生效的时候,使用了方法二。
头部的html不需要任何的声明,都去掉,如下面代码所示:
iframe高度自适应