小编典典

使iframe根据内容自动调整高度,而无需使用滚动条?

html

例如:

<iframe name="Stack" src="http://stackoverflow.com/" width="740"
        frameborder="0" scrolling="no" id="iframe"> ...
</iframe>

我希望它能够根据其中的内容调整高度,而无需使用滚动。


阅读 735

收藏
2020-05-10

共1个答案

小编典典

将此添加到您的<head>部分:

<script>
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.documentElement.scrollHeight + 'px';
  }
</script>

并将您的iframe更改为此:

<iframe src="..." frameborder="0" scrolling="no" onload="resizeIframe(this)" />
2020-05-10