简体   繁体   English

$('')。css('height')返回计算的高度,而不是JQuery中的CSS高度

[英]$('').css('height') is returning computed height, not CSS height in JQuery

I defined the height property of an element using CSS. 我使用CSS定义了一个元素的height属性。 If the computed height is different from the defined height, then $(element).css('height') returns the computed height, not the height that I defined in CSS. 如果计算出的高度与定义的高度不同,则$(element).css('height')返回计算出的高度,而不是我在CSS中定义的高度。 I thought that css() returns the height defined in CSS. 我认为css()返回CSS中定义的高度。

Here's a fiddle to show what I mean: http://jsfiddle.net/D9rQR/ I expected css() to return 100px , but it's returning 20px . 这里有一个小提琴来显示我的意思: http : //jsfiddle.net/D9rQR/我期望css()返回100px ,但是返回20px I'm using Chrome. 我正在使用Chrome。

How do get the defined height ( 100px in the fiddle) using JQuery/Javascript? 如何使用JQuery / Javascript获得定义的高度(小提琴中为100px )?

Edit: In the example, I set a max-height to emulate what happens in browsers that don't support height for select s. 编辑:在该示例中,我设置了一个max-height来模拟在不支持select s height浏览器中发生的情况。 I'm using the following to check if the browser computes the height properly: 我正在使用以下方法检查浏览器是否正确计算了高度:

if($('select').outerHeight()<parseInt($('select').css('height'))){
    // browser doesn't support "height" for "select"s
}

However, this doesn't work because $('select').css('height') always returns the computed height. 但是,这不起作用,因为$('select').css('height')始终返回计算出的高度。

$('select').height() is height of select without padding $('select').height()是选择的高度,不带填充

$('select').css('height') is actual height (has padding) $('select').css('height')是实际高度(有填充)

$('select').css('height') = $('select').outerHeight(); $('select').css('height') = $('select').outerHeight();

If you add border:0; 如果添加border:0; and remove max-height you will get the correct height, as height does not get applied on selects unless border is 0, not sure if this also applies to ie. 并删除max-height您将获得正确的高度,因为除非边界为0,否则高度不会应用于选择项,请不确定这是否也适用于ie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM