gpt4 book ai didi

javascript - 向 .height() 添加一个数字

转载 作者:太空宇宙 更新时间:2023-11-03 20:07:35 25 4
gpt4 key购买 nike

我有这个简单的 Jquery,目的是将 .button 的高度设置为其同级按钮的高度。

$('.button').height($('.sibling').height()); 

.sibling 的实际高度是 48px(24px + 24px padding,顶部和底部)。

但是 .sibling 的返回高度不包括填充,将 .button 设置为 24px,而不是 48px。

我试过了

$('.button').height($('.sibling').height() + "24" );

但是返回 2412px

执行此操作的正确方法是什么?我看到要么更改原始文件以某种方式告诉它获取高度和填充,否则在这种情况下如何在 Jquery 中进行适当的数学运算?

最佳答案

问题是您将数学运算与串联混合在一起。

尝试像这样分开它们:

var newHeight = $('.sibling').height() + 12;
$('.button').css('height', newHeight + 'px');

或者:

var newHeight = $('.sibling').height() + 12;
$('.button').height(newHeight);

关于javascript - 向 .height() 添加一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43467244/

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