[英]Can't get the height of a div element
It's a stupid question but I can't seem to figure it out. 这是一个愚蠢的问题,但我似乎无法弄明白。 Basically I want the height of my div element.
基本上我想要div元素的高度。 But some how it keeps returning 0. When i inspect the element in Chrome it gives me a height.
但是一些如何保持返回0.当我检查Chrome中的元素时,它给了我一个高度。 Code:
码:
HTML HTML
<div id="signup">
<div class="panel">
</div>
</div>
CSS: CSS:
.panel
{
width: 90%;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-ms-border-radius: 10px;
-o-border-radius: 10px;
border-radius: 10px;
background: #0071bc;
padding: 1em;
border: 0.3em solid;
border-color: #073b73;
color: #ffffff;
position: relative;
left:0;
right: 0;
margin: auto;
}
JQuery: JQuery的:
alert($("#signup .panel").outerHeight());
alert($("#signup .panel").height());
I have used the above lines in the following methods: 我在以下方法中使用了上述行:
$(document).ready(function(){
});
$(window).load(function(){
});
Every time I try to alert the height I just keep getting 0. What I have noticed is that in the CSS if I specify a height, ie height:361px;
每当我尝试提醒身高时我就会一直得到0.我注意到的是,如果我指定一个高度,即CSS
height:361px;
or height:40%;
或
height:40%;
I get 361 or 40. But I dont want this. 我得到361或40但我不想要这个。 My panels height changes based on the elements with in it.
我的面板高度根据其中的元素而变化。 I want signin's height once its rendered.
一旦渲染,我想要signin的高度。 I've now tried... I have no idea how to do this.
我现在试过......我不知道怎么做。 Please help.
请帮忙。
Why height returns as "0" with no data: 为什么高度在没有数据的情况下返回为“0”:
This seems right to me. 这对我来说似乎是对的。
height()
returns the height without padding and margin. height()
返回没有填充和边距的高度。 And because the element has no content ( data ) the height is returned as 0. 并且因为元素没有内容( 数据 ),所以高度返回为0。
Why it works with outerHeight: 为什么它适用于outerHeight:
The reason chrome shows a height is because you have padding and borders. chrome显示高度的原因是因为您有填充和边框。 Which is why you get a value for
outerheight()
. 这就是为什么你得到
outerheight()
的值。 OuterHeight()
will return the height with padding and borders. OuterHeight()
将返回带填充和边框的高度。 And if you use outerHeight(true)
you will get the height of the element plus the padding, border, and margin. 如果你使用
outerHeight(true)
你将获得元素的高度加上填充,边框和边距。
NOTE: 注意:
If you want the height of an element including padding but not the border. 如果你想要一个元素的高度,包括填充,但不是边框。 You would use
innerHeight()
. 你会使用
innerHeight()
。 and innerHeight(true)
if you want padding and margin height without border height. 和
innerHeight(true)
如果你想要填充和边距高度没有边框高度。
See Documentation : 见文档 :
The code as is works, as shown in this fiddle . 代码是有效的,如这个小提琴所示。 If the code is not functioning, the problem is elsewhere.
如果代码不起作用,则问题出在其他地方。 Make sure jQuery is linked, and there are no errors in the console.
确保jQuery已链接,并且控制台中没有错误。
The fiddle HTML: 小提琴HTML:
<div id="signup">
<div class="panel">
Tons of Content<br>
Even More Content
</div>
</div>
The fiddle jQuery: 小提琴jQuery:
$(document).ready(function(){
console.log($("#signup .panel").height());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.