gpt4 book ai didi

javascript - 比较 JQuery 函数中的负数

转载 作者:行者123 更新时间:2023-11-28 02:01:45 25 4
gpt4 key购买 nike

我试图比较两个负数,但比较失败。在这种特定情况下,getLeftPercent() 是负数 (-14),但在比较时,会执行操作 B。从逻辑上讲,(-14) 小于 (-10),因此应该执行操作 A。

如果我将右侧比较操作数 (-10) 更改为正数(例如 1),则执行操作 A。 JavaScript 是否有一些我忽略的怪癖(它就在我面前)?我可以发布完整的代码,但没有更多的了。

$(function() {

// the statement in question
if (parseInt(getLeftPercent(), 10) < -10) {
// do action A
// this is just a debug statement
$('#posbanner').html('element position is less than negative 10');
} else {
// do action B
// this is just a debug statement
$('#posbanner').html('element position is greater than or equal to negative 10');
}
});

// element left position on page
function getLeftPercent() {
var leftStr = $('#sidebar').css('left'),
pxPos = leftStr.indexOf('px'),
leftVal = leftStr.substr(0, pxPos),
leftPercent = (leftVal / $(window).width() * 100).toString(),
dotPos = leftPercent.indexOf('.'),
leftPercentStr = dotPos == -1 ? leftPercent : leftPercent.substr(0, dotPos);
return leftPercentStr;
};

最佳答案

我尝试在 jsfiddle 上运行以下代码

if (parseInt( -14, 10) < -10)
{
alert("I am in A");
}
else
{
alert("I am in B");
}

我明白我在A不确定 getLeftPercent() 是否返回您期望的值。

关于javascript - 比较 JQuery 函数中的负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18341769/

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