gpt4 book ai didi

javascript - jS : On click function add number to variable - gives NaN

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:39 25 4
gpt4 key购买 nike

我的问题很简单,我无法使用 click 事件添加到 javascript 中的全局变量。它返回一个 NaN 结果。

var xx = 50;

$('div').click(function(){
var xx = xx + 10;
alert(xx)
});

JSFIDDLE

最佳答案

因为您要声明同一个变量 (xx) 两次

参见 fiddle :https://jsfiddle.net/1beo7mgw/2/

So, why it is returning NaN?

因为在这一行 var xx = xx + 10; 变量 xx 被再次定义但没有用任何值加上 10 进行初始化。

所以 var xx 将保留 undefined 并且在 undefined 上加 10 将返回非数字 (NaN)

关于javascript - jS : On click function add number to variable - gives NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441315/

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