我试图调试它,但我只是不明白为什么每当我将变量disp加1时变量disp就返回NaN。如果有人能阐明为什么会发生这种情况,我将十分感激。
var votePages = new Array();
votePages[0] = "http://minecraftservers.org/vote/100924";
votePages[1] = "http://www.planetminecraft.com/server/revolutionarycraft---ssdfactionsecosurvivalgrief/vote/";
votePages[2] = "http://minecraftservers.net/server/66267/vote/";
votePages[3] = "http://mineservers.com/server/11885/vote";
votePages[4] = "https://minestatus.net/92187-revolutionary-craft/vote";
var disp = 0;
$("#dispVotePage").attr("src", votePages[disp]);
$(document).on('click', '#next', function(){
$("#dispVotePage").attr("src", '');
var disp = disp++;
alert(disp);
$("#dispVotePage").attr("src", votePages[disp]);
if (disp !== 0) {
$("#previous").css('display', 'block');
}
});
$(document).on('click', '#previous', function(){
$("#dispVotePage").attr("src", '');
var disp = disp--;
$("#dispVotePage").attr("src", votePages[disp]);
if (disp === 0) {
$("#previous").css('display', 'none');
}
});
您会看到我那里有alert(disp),它用于调试。
最佳答案
重新声明disp
。您两次声明:
var disp = 0; //Initial Declaration (Global)
... <snip> ...
$(document).on('click', '#next', function(){
$("#dispVotePage").attr("src", '');
var disp = disp++; //Re-declaration. Declares `disp` as a new local variable.
alert(disp);
$("#dispVotePage").attr("src", votePages[disp]);
if (disp !== 0) {
$("#previous").css('display', 'block');
}
});
解决方案:要使用全局
disp
,请勿再次声明。只需使用disp++
关于javascript - 在javascript中递增整数会导致NaN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19599616/