jQuery在应用类之前计算高度
我试图让jQuery计算元素的高度,然后添加一个类,我将使用该类在CSS中将元素的高度设置为0px 我的问题是jQuery没有按照正确的顺序进行这些计算。这就是我希望发生的事jQuery在应用类之前计算高度,jquery,css,Jquery,Css,我试图让jQuery计算元素的高度,然后添加一个类,我将使用该类在CSS中将元素的高度设置为0px 我的问题是jQuery没有按照正确的顺序进行这些计算。这就是我希望发生的事 $(document).ready(function(){ elementHeight = $('div#test').outerHeight(true); // Calculate the height of the element before we do anything else }, $('div#test'
$(document).ready(function(){
elementHeight = $('div#test').outerHeight(true); // Calculate the height of the element before we do anything else
}, $('div#test').addClass('zeroheight')); // Once we've calculated add a CSS class
CSS
#test {
height: 0;
}
现在,假设自然元素高度为300px-变量elementHeight应该返回300px,因为我只是在执行addClass之后应用一个类来更改高度
但它返回的是0px
在我添加一个类将高度设置为0之前,有没有人能给我一个解决方案来计算300px的值
非常感谢
$(document).ready(function(){
var elementHeight = $('#test').height();
$('#test').addClass('zeroheight');
});
您的javascript中有语法错误,没有正确关闭docready…这是有效的,我不知道为什么我一开始没有尝试,但谢谢!