在类更改后重新计算jQuery中的元素高度

Gre*_*ude 6 css jquery height css3

我有一个项目列表,根据标准,它通过jQuery on document.ready获取一个类,触发CSS3列.

如果列表以列显示,则其高度将更小.有没有办法在课程更改后立即在jQuery中获得新的高度?

$items.each(function(i){

var theItem = this;

console.log($(theItem).height());

//extended layout

if ( theCriteria ) {
    $(theItem).addClass('extended'); 
    console.log('after', $(theItem).height()); }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码返回两个调用的初始高度.我猜我需要触发别的东西.

Ale*_*ton 9

很多时候,在函数闭包完成之前不会发生dom操作.

关于这个问题的一篇好文章:http://www.quirksmode.org/blog/archives/2009/08/when_to_read_ou.html

最好是进行setTimeout函数调用而不是直接日志.

代替:

console.log('after', $(theItem).height());
Run Code Online (Sandbox Code Playgroud)

尝试

setTimeout(function(){ console.log('after', $(theItem).height()); }, 0);
Run Code Online (Sandbox Code Playgroud)

设置超时0将使其尽快运行,同时仍然在当前正在运行的功能之后.

希望这是你的问题.祝好运.