从Jquery 1.8开始,获取元素的height()时发生了变化。我有一个CSS div的高度设置为auto,图像内部通过使用%和auto来指定div的高度和宽度),当窗口加载时,我使用Jquery获取元素的高度并在其旁边创建另一个div相同的高度。经过研究,我已经注意到它在CSS设置了由图像设置的新高度之前返回高度。 1.7允许这个,但1.8和以上不会。它是一种解决方法。 这是CSSjquery 1.8及更高版本,将div的高度设置为与另一个div相同
#element1{ width:80%; height:auto;}
#element1 img{width:100%; height:auto};//this allows the image to resize with the page responsively.
jQuery的...
$(window).ready(
function(){
var x = $("#element").height();
alert(x); // this would return what the height was dynamically set as by the css in 1.7, but 1.8 returns a small number that i am pretty certain is just the padding added to 0px
});
希望这是有道理的,有人有一个变通。 谢谢
如果你设置了一个小提琴我可以帮助你更多,但尝试'.outerHeight()'? – elzi 2014-11-06 22:19:01
'.outerHeight()'是一个很好的建议;这包括高度+边框+边距+填充 – 2014-11-06 22:26:13