gpt4 book ai didi

javascript - 将 outerHeight 与 padding 相加给出错误的结果

转载 作者:行者123 更新时间:2023-11-28 16:12:10 25 4
gpt4 key购买 nike

我想将一个 div 的 outerheight() 值与另一个 div 的 padding-top 值相加。这是我的功能

    var headerHeight = $('header').outerHeight() + "px";
console.log(headerHeight);
var containerPT = $(".container").css('padding-top');
console.log(containerPT);
var totalSpace = (headerHeight + containerPT);
console.log(totalSpace);

结果是:

//headerHeight 
474px
//containerPT
150px
//totalSpace
474px150px

我需要的结果当然是474 + 150 = 624px;我通常很容易总结东西,我不确定为什么会这样以及我做错了什么。非常感谢任何帮助。

最佳答案

您不会得到正确的结果,因为您添加的是 2 个字符串而不是 2 个数字。你应该做的是将它们转换成数字以获得正确的结果。为此,请使用 parseInt() 函数。

var totalSpace = (parseInt(headerHeight) + parseInt(containerPT));
console.log(totalSpace + "px");

希望这能给您带来想要的结果。

关于javascript - 将 outerHeight 与 padding 相加给出错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38173285/

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