2010-01-04 163 views
6

我有一个项目列表,根据标准,它通过document.ready上的jQuery获得一个类,触发CSS3列。重新计算jQuery在元素更改后的元素高度

如果列表以列显示,它将具有较小的高度。有没有什么方法可以在课堂更改后立即在jQuery中获得新的高度?

$items.each(function(i){ 

var theItem = this; 

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

//extended layout 

if (theCriteria) { 
    $(theItem).addClass('extended'); 
    console.log('after', $(theItem).height()); } 
} 

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

+0

难道你不能只检查改变类的代码的高度? – 2010-01-04 07:04:19

+0

不行,它不起作用。我会在一秒内发布代码 – GreenDude 2010-01-04 07:08:33

回答

9

很多时候,dom操作不会发生,直到函数关闭完成。

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

这可能是最好做一个setTimeout函数调用,而不是直接的日志。

代替:

console.log('after', $(theItem).height()); 

尝试

setTimeout(function(){ console.log('after', $(theItem).height()); }, 0); 

超时设置为0将使其尽早运行,而仍是正在运行的当前功能之后。

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

+0

作品,欢呼! :) – GreenDude 2010-01-04 08:23:23