Javascript jQuery $("body").height() 返回未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11023183/
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-24 04:04:49  来源:igfitidea点击:

jQuery $("body").height() returns undefined

javascriptjquery

提问by dmck

I have a call like this:

我有一个这样的电话:

$("#ContextMenuModal").height($("body").height());

However $("body").height()returns undefined when I view it in Firebug.

但是,$("body").height()当我在 Firebug 中查看它时返回 undefined。

What would cause jQuery to return undefined for the body height?

什么会导致 jQuery 返回 undefined 的身高?

I am using jQuery 1.4.1.

我正在使用 jQuery 1.4.1。

Edit:

编辑:

This is inside an iFrame

这是在 iFrame 内

回答by Norse

Simply use

只需使用

$(document).height() // - $('body').offset().top

and / or

和/或

$(window).height()

instead $('body').height()

相反 $('body').height()

To expand a bit,

稍微展开一下,

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document

As ba?megakapa points out, there is a slight difference, albeit a few pixels. The true height of the body can be calculated by subtracting the body offset from the document height (like I mentioned above):

正如 ba?megakapa 指出的那样,虽然只有几个像素,但还是有细微的差别。身体的真实高度可以通过从文档高度中减去身体偏移来计算(就像我上面提到的):

$(document).height() - $('body').offset().top

回答by Ash

Use this-

用这个-

 $("body").css("height")

well i will say jquery is not needed-

好吧,我会说不需要 jquery-

this.style.height;//it will not work if you add it as inline js.