如何同步iframe与嵌入内容的高度

最近频繁的作一些经过iframe在a页面嵌入b页面需求。总结下来,有如下问题须要解决css

1.如何同步iframe与嵌入内容的高度浏览器

2.将b页面载入到a页面后,如何隐藏掉b页面上的元素,如左导航,顶部导航等等dom

 

-如何同步iframe与嵌入内容的高度spa

a)获取由iframe引入的页面高度code

contentWindow返回的是嵌入到中页面的window对象。能够经过这个window对象获取到网页的高度。contentWindow支持全部主流浏览器。对象

contentDocument返回的是嵌入页面的document对象。blog

 

b)同步引入页面与iframe的高度ip

 将获取到的高度赋值给iframeget

 demoiframe

  <iframe src="http://jsbin.com/nobefis" id="currentFrame" width="100%" scrolling="no" frameborder="no"></iframe>
  
  <script>
      function resizeFrameHeight(currentFrame){
        if(currentFrame){
          var iframeObj = currentFrame.contentWindow;//获取iframe引入网页的window对象,进而经过window对象获取引入内容的document对象
      if(iframeObj.document.body){   currentFrame.height = iframeObj.document.body.scrollHeight; } } } window.onload = function () { resizeFrameHeight(document.getElementById("currentFrame")) } </script>

 

HTMLIFrameElement.contentWindow

window.frames

HTML IFrameElement

iframe

contentWindow for an iframe

js操做iframe里的dom

相关文章
相关标签/搜索