如何使用 jQuery 从 iFrame 中获取父窗口的高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/735072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:40:08  来源:igfitidea点击:

How can I get a parent window's height from within iFrame using jQuery?

jquerydomiframe

提问by Fred Wilson

Within an iFrame, we need to get the parent's window/document height.

在 iFrame 中,我们需要获取父窗口/文档的高度。

Is there a way to get this using jQuery?

有没有办法使用 jQuery 得到这个?

I know we can use $(document).height() to get a page's height. I just can't figure out how to get the parent's value from within the iFrame.

我知道我们可以使用 $(document).height() 来获取页面的高度。我只是不知道如何从 iFrame 中获取父级的值。

Please help!

请帮忙!

回答by Thinker

jQuery is not needed.

不需要 jQuery。

parent.document.body.clientHeight

parent.document.body.clientHeight

That works for me with IE7 and FF3.

这对我来说适用于 IE7 和 FF3。

回答by Kld

In jQuery you can do

在 jQuery 你可以做

$(parent.window).width();

回答by Shai Reznik - HiRez.io

I tried the clientHeight approach on a website where both Iframes where on the same domain and it didn't work. (return 0).

我在一个网站上尝试了 clientHeight 方法,其中两个 Iframe 都在同一个域中,但它不起作用。(返回 0)。

After a lot of testing the best way I have found (and I'll be happy to learn a better way) is to create a function on the parent which returns the document height, something like:

经过大量测试后,我发现的最佳方法(我很乐意学习更好的方法)是在父级上创建一个返回文档高度的函数,例如:

Parent:

家长:

function getDocumentHeight(){
   return $(document).height();
}

Iframe:

框架:

var parentDocHeight = parent.getDocumentHeight();

回答by Elo

Another possibility is :

另一种可能性是:

$(window.parent.document).height()