gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:18:41 25 4
gpt4 key购买 nike

我有一个元素列表,根据一个标准,它通过 jQuery 在 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 将使其尽快运行,同时仍在当前正在运行的函数之后。

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

关于jquery - 类更改后在 jQuery 中重新计算元素高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1997887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com