我是一个初学者,所有的编码。这是我的总目标。我运行的东西相对简单,我有8个超级英雄在屏幕上。我希望用户消除屏幕上的4个DC超级英雄,在他们全部从屏幕上被淘汰后,我希望系统提醒用户,他们已经赢得了游戏。他们不需要按任何顺序来做,所以每次点击DC字符时,我都运行superHero函数来检查是否已经消除了所有四个DC超级英雄。谁来帮帮我。我觉得这是一件很简单的事情,我正在搞砸。提前一吨谢谢。
/*This is my jquery that shows all 8 of my superheroes*/
$('#heroes').show();
var flashHidden = !$('#greenlantern').is(':visible');
var greenHidden = !$('#greenlantern').is(':visible');
var batmanHidden = !$('#batman').is(':visible');
var supermanHidden = !$('#superman').is(':visible');
function superHero() {
if(flashHidden && batmanHidden && supermanHidden && greenHidden) {
alert ("Congratulations!!! You have won the game!! Please proceed forward and fill out a quick survey for the developers");
}
}
$('#flash').click(function(){
$('#flash').hide('slow',function(){
superHero();
});
});
$('#greenlantern').click(function(){
$('#greenlantern').hide('slow',function(){
superHero();
});
});
$('#batman').click(function(){
$('#batman').hide('slow',function(){
superHero();
});
});
$('#superman').click(function(){
$('#superman').hide('slow',function(){
superHero();
});
});
});现在,正在发生的事情是,我将消除所有正确的超级英雄,它不会提醒我,用户已经赢了。我已经尝试了很多不同的事情,唯一的其他结果是让系统提醒用户,每次他们点击一个超级英雄,他们赢了,这也是不正确的。
编辑
通过将变量的范围更改为函数中的变量,解决了这一问题。
发布于 2014-01-22 19:52:46
您应该在函数中声明变量,即flashHidden。目前,您正在设置然后在开始。
function superHero() {
var flashHidden = !$('#flash').is(':visible');
var greenHidden = !$('#greenlantern').is(':visible');
var batmanHidden = !$('#batman').is(':visible');
var supermanHidden = !$('#superman').is(':visible');
if(flashHidden && batmanHidden && supermanHidden && greenHidden) {
alert ("Congratulations!!! You have won the game!! Please proceed forward and fill out a quick survey for the developers");
}
}此外,您的单击处理程序可以压缩为
$('#flash, #greenlantern, #batman, #superman').click(function(){
$(this).hide('slow',function(){
superHero();
});
});发布于 2014-01-22 19:52:56
你在设置..。
var flashHidden = !$('#greenlantern').is(':visible');...to开始于。但是,当该变量被隐藏时,您以后不会更新它。因此,根据你的支票:
if(flashHidden && batmanHidden && supermanHidden && greenHidden) {...Flash仍然可见。即使,是的,在那页上他已经走了。
尝试添加以下内容:
$('#flash').click(function(){
$('#flash').hide('slow',function(){
flashHidden=true;
superHero();
});
});https://stackoverflow.com/questions/21292517
复制相似问题