gpt4 book ai didi

javascript - jquery .css 函数不稳定行为问题

转载 作者:行者123 更新时间:2023-11-30 13:10:00 24 4
gpt4 key购买 nike

我对 jquery .css 函数有疑问。我正在使用它来获取高度设置为自动的元素的实际高度。我正在使用的代码是:

$(this).css({ height: $(this).css("height"), width: $(this).css("width") });

问题是,.css 函数有时会返回 "0px" 有时会返回 "auto" 有时会返回实际高度(这是所需的行为)。谁能告诉我我在这里做错了什么?

最佳答案

$(this).css("height") 为您提供定义(或默认)值,而 $(this).height() 为您提供实际,“测量”高度。

来自 http://api.jquery.com/height/ :

Get the current computed height for the first element in the set of matched elements or set the height of every matched element.

来自http://api.jquery.com/css/ :

Get the value of a style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.

关于javascript - jquery .css 函数不稳定行为问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14242400/

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