Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery在应用类之前计算高度_Jquery_Css - Fatal编程技术网

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'

我试图让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').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…这是有效的,我不知道为什么我一开始没有尝试,但谢谢!