Javascript 动态调整Iframe高度,并在客户端事件上更改内容高度时设置滚动(使用+;/-按钮展开/折叠文本)

Javascript 动态调整Iframe高度,并在客户端事件上更改内容高度时设置滚动(使用+;/-按钮展开/折叠文本),javascript,iframe,scroll,height,Javascript,Iframe,Scroll,Height,我一直在寻找一个解决方案,使用javascript设置同一个域页面的iframe滚动。我只想只有当内容的高度大于窗口高度时才在iframe上滚动。遇到使用setInterval的解决方案。在IE中,对于隐藏了某些内容的页面,我无法获得适当的滚动高度。该作品在铬和莫兹拉虽然罚款。另外,看起来我必须设置iframe的max-hright,以便在使用iframe页面中单击+/-时,在chrome和IE上运行此代码。我无法控制iframe的源页面。还有一些事件,在客户端,iframe的内容高度发生了变化

我一直在寻找一个解决方案,使用javascript设置同一个域页面的iframe滚动。我只想只有当内容的高度大于窗口高度时才在iframe上滚动。遇到使用setInterval的解决方案。在IE中,对于隐藏了某些内容的页面,我无法获得适当的滚动高度。该作品在铬和莫兹拉虽然罚款。另外,看起来我必须设置iframe的max-hright,以便在使用iframe页面中单击+/-时,在chrome和IE上运行此代码。我无法控制iframe的源页面。还有一些事件,在客户端,iframe的内容高度发生了变化,比如展开/折叠。任何关于发生原因的建议或任何其他实施方案

Iframe容器(div)

Iframe样式

    top: 20px;margin:0;padding:0
Javascript函数,该函数根据间隔调用以将iframe高度设置为该内容高度

    function AdjustIframeHeightOnLoad() {


var screenHeight = $(window).height();
var iframeContentHeight;

var iframeContentwidth;

iframeContentHeight = document.getElementById("iframe_ObjectLink").contentWindow.document.body.scrollHeight;

iframeContentwidth = document.getElementById("iframe_ObjectLink").contentWindow.document.body.scrollWidth;


if (iframeContentHeight > (screenHeight - 47)) {
    document.getElementById("objectionMenuContentContainer_div").style.height = (screenHeight - 27) + "px";
  document.getElementById("iframe_ObjectLink").height = (screenHeight - 47);
    document.getElementById("iframe_ObjectLink").contentWindow.document.documentElement.style.overflowY = "scroll";
}
else {
    document.getElementById("objectionMenuContentContainer_div").style.height = iframeContentHeight + "px";
    document.getElementById("iframe_ObjectLink").height = (iframeContentHeight - 25);
    document.getElementById("iframe_ObjectLink").contentWindow.document.documentElement.style.overflowY = "hidden";
}

if (iframeContentwidth > 883)
    document.getElementById("iframe_ObjectLink").contentWindow.document.documentElement.style.overflowX = "scroll";
else
    document.getElementById("iframe_ObjectLink").contentWindow.document.documentElement.style.overflowX = "hidden";

}

您能告诉我们您尝试的代码吗?iFrame必须是固定高度或基于百分比的高度。虽然您可以动态更改iframe高度,但实际上无法基于内容的高度进行更改,因为您无法通过iframe获取源文档的高度。从技术上讲,iframe中的任何内容都不是页面DOM的一部分,只有iframe本身才是。换句话说,如果没有某种API在DOM呈现之前获取其他站点上内容的高度,就无法获取iframe内容高度。简言之,iframes在造型方面很差劲。你能告诉我们你试过的代码吗?iframes必须是固定高度或基于百分比的高度。虽然您可以动态更改iframe高度,但实际上无法基于内容的高度进行更改,因为您无法通过iframe获取源文档的高度。从技术上讲,iframe中的任何内容都不是页面DOM的一部分,只有iframe本身才是。换句话说,如果没有某种API在DOM呈现之前获取其他站点上内容的高度,就无法获取iframe内容高度。简言之,iframes在造型方面很差劲。
    function AdjustIframeHeightOnLoad() {


var screenHeight = $(window).height();
var iframeContentHeight;

var iframeContentwidth;

iframeContentHeight = document.getElementById("iframe_ObjectLink").contentWindow.document.body.scrollHeight;

iframeContentwidth = document.getElementById("iframe_ObjectLink").contentWindow.document.body.scrollWidth;


if (iframeContentHeight > (screenHeight - 47)) {
    document.getElementById("objectionMenuContentContainer_div").style.height = (screenHeight - 27) + "px";
  document.getElementById("iframe_ObjectLink").height = (screenHeight - 47);
    document.getElementById("iframe_ObjectLink").contentWindow.document.documentElement.style.overflowY = "scroll";
}
else {
    document.getElementById("objectionMenuContentContainer_div").style.height = iframeContentHeight + "px";
    document.getElementById("iframe_ObjectLink").height = (iframeContentHeight - 25);
    document.getElementById("iframe_ObjectLink").contentWindow.document.documentElement.style.overflowY = "hidden";
}

if (iframeContentwidth > 883)
    document.getElementById("iframe_ObjectLink").contentWindow.document.documentElement.style.overflowX = "scroll";
else
    document.getElementById("iframe_ObjectLink").contentWindow.document.documentElement.style.overflowX = "hidden";