Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态Iframe调整大小和窗口焦点滚动错误_Javascript_Iframe_Resize - Fatal编程技术网

Javascript 动态Iframe调整大小和窗口焦点滚动错误

Javascript 动态Iframe调整大小和窗口焦点滚动错误,javascript,iframe,resize,Javascript,Iframe,Resize,所以我一直在到处寻找适合我的解决方案,我几乎找到了,但我遇到了一个小故障。由于产品的限制,我需要在iFrame中加载某些页面。我无法安装任何其他库 我的子页面有一些div,单击这些div会隐藏和取消隐藏,因此内容的大小会发生变化。这个代码几乎可以工作,高度会扩大,但不会再次收缩 <script> function checkFocus() { if(document.activeElement == document.getElementsByTagName("ifra

所以我一直在到处寻找适合我的解决方案,我几乎找到了,但我遇到了一个小故障。由于产品的限制,我需要在iFrame中加载某些页面。我无法安装任何其他库

我的子页面有一些div,单击这些div会隐藏和取消隐藏,因此内容的大小会发生变化。这个代码几乎可以工作,高度会扩大,但不会再次收缩

<script>
function checkFocus() {
  if(document.activeElement == document.getElementsByTagName("iframe")[1]) {
    test.style.height = test.contentWindow.document.documentElement.scrollHeight + 'px';
  }
}

window.setInterval(checkFocus, 1000); 
</script>

<iframe id="test" src="/SitePages/test3.aspx" onload='javascript:(function(o){o.style.height=o.contentWindow.document.body.scrollHeight+"px";}(this));' style="height:50px;width:100%;border:none;overflow:hidden;"></iframe>
之前:

test.style.height = test.contentWindow.document.documentElement.scrollHeight + 'px';
然后子页面又收缩了,除了我的主页上有一个奇怪的滚动错误,它不断地迫使页面向上滚动。有没有办法解决这个问题


谢谢你的阅读

我知道了。添加

window.focus();
之后

修正了这个问题。这只是将焦点从iframe上移开,并将其放回主窗口,因为它正在循环

window.focus();
test.style.height = test.contentWindow.document.documentElement.scrollHeight + 'px';