0

根据其中的内容获取和设置 iframe。默认高度为 150 像素。我尝试使用 document.documentelement.clientheight 来获取高度,

4

2 回答 2

0

我认为您可以创建两个函数,一个在您创建 iframe 的文档中,另一个在您在 iframe 中显示的文档中,例如:

父功能:

$.fn.setSizeIframe = function(height,width) {

  $(#idIframe).width(width);
  $(#idIframe).height(height);

}

在此函数中,您将收到两个参数(高度和宽度),这些参数将由调用 setSizeIframe 函数的函数传递,在这种情况下,该函数将位于子文档或您将显示的文档中在 iframe 中。

子功能:

$.fn.mySize = function(){

  let height = $("body").height();
  let width = $("body").width();

  parent.$.fn.setSizeIframe(height,width);

}

当文档准备好(子文档)时必须调用此函数。

于 2018-09-13T17:31:31.097 回答
0

首先,您需要获取 iframe 元素。然后将 iframe 的高度设置为 iframe 内内容的主体高度。

const iframe = document.getElementById('myIframe');

iframe.style.height = iframe.contentWindow.document.body.offsetHeight;
于 2018-09-13T17:33:46.903 回答