Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

javascript - 根据内部内容使iframe高度动态-JQUERY / Javascript(make iframe height dynamic based on content inside- JQUERY/Javascript)

I am loading an aspx web page in an iframe.(我正在iframe中加载aspx网页。)

The content in the Iframe can be of more height than the iframe's height.(iframe中的内容可以比iframe的高度更高。) The iframe should not have scroll bars.(iframe不应该包含滚动条。) I have a wrapper div tag inside the iframe which basically is all the content.(我在iframe中有一个包装器div标签,基本上是所有内容。) I wrote some jQuery to make the resize happen :(我写了一些jQuery来调整大小:) $("#TB_window", window.parent.document).height($("body").height() + 50); where TB_window is the div in which the Iframe is contained.(其中TB_window是包含Iframe的div。) body - the body tag of the aspx in the iframe.(body -iframe中aspx的body标签。) This script is attached to the iframe content.(该脚本已附加到iframe内容中。) I am getting the TB_window element from the parent page.(我从父页面获取TB_window元素。) While this works fine on Chrome, but the TB_window collapses in Firefox.(尽管此功能在Chrome上运行良好,但TB_window在Firefox中崩溃了。) I am really confused/lost on why that happens.(我真的很困惑/迷茫,为什么会这样。)   ask by karry translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can retrieve the height of the IFRAME 's content by using: contentWindow.document.body.scrollHeight(您可以使用以下方法检索IFRAME内容的高度: contentWindow.document.body.scrollHeight)

After the IFRAME is loaded, you can then change the height by doing the following:(加载IFRAME后,可以执行以下操作来更改高度:) <script type="text/javascript"> function iframeLoaded() { var iFrameID = document.getElementById('idIframe'); if(iFrameID) { // here you can make the height, I delete it first, then I make it again iFrameID.height = ""; iFrameID.height = iFrameID.contentWindow.document.body.scrollHeight + "px"; } } </script> Then, on the IFRAME tag, you hook up the handler like this:(然后,在IFRAME标记上,像这样连接处理程序:) <iframe id="idIframe" onload="iframeLoaded()" ... I had a situation a while ago where I additionally needed to call iframeLoaded from the IFRAME itself after a form-submission occurred within.(不久前,我遇到了一种情况,即在其中发生表单提交后,我还需要从IFRAME本身调用iframeLoaded 。) You can accomplish that by doing the following within the IFRAME 's content scripts:(您可以通过在IFRAME的内容脚本中执行以下操作来实现:) parent.iframeLoaded();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...