gpt4 book ai didi

javascript - 尝试添加/减去变量

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

我试图有一些东西,当它启用时,它会添加到变量中,当它被禁用时,它会从变量中减去,但是当我警报时,变量的值只是出现说NaN 这是我的代码:

这用于启用/禁用按钮

var speedrating;
function onoffButton(i, g, r)
{
$(i).click(function () {
if ($(i).html() == '<a class="onoffswitch" id="' + g + 'ON"></a>') {
$(i).html('<a class="onoffswitch" id="' + g + 'OFF"></a>');
speedrating -= r;
} else {
speedrating += r;
$(i).html('<a class="onoffswitch" id="' + g + 'ON"></a>');
}
updateSpeedRate();
});
}

这里是检查变量以查看要执行的操作的位置:

function updateSpeedRate()
{
if (speedrating < 34) {
$('.speedmeter').css('background-position', '0px -30px');
} else {
if (speedrating < 67) {
$('.speedmeter').css('background-position', '0px -15px');
} else {
$('.speedmeter').css('background-position', '0px 0px');
}
}
alert(speedrating);
}

然后使用启用/禁用对象(第三个数字是我要使用的数字):

onoffButton("#switchRating", "rating", 10);

onoffButton("#switchFavicon", "favicon", 30);

onoffButton("#switchClock", "clockswitch", 10);

onoffButton("#switchWeather", "weatherswitch", 30);

onoffButton("#switchNotepad", "notepadswitch", 15);

onoffButton("#switchLinks", "linksswitch", 5);

最佳答案

您需要先给 speedRating 一个值,然后再添加/减去它

var speed rating = 0;

关于javascript - 尝试添加/减去变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6260066/

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