在类更改后重新计算jQuery中的元素高度
我有一个项目列表,根据标准,它通过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()); } }
上面的代码返回两个调用的初始高度。 我猜我需要触发别的东西。
很多时候,在函数闭包完成之前不会发生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
将使其尽快运行,同时仍然在当前正在运行的function之后运行。
希望这是你的问题。 祝好运。