【发布时间】:2016-04-06 10:18:31
【问题描述】:
问题
我在页面底部有一个resetQuiz() 测验按钮,该按钮应该将分数设置回0,但如果该人得到25个正确答案,请单击.button__reset,然后再次进行测验,它变为 26 而不是 0。
直播链接:https://s3.amazonaws.com/bsunproduction/yearinreview/index.html
scripts.js
/*-------------------------------------
QUIZ
--------------------------------------*/
// Keep track of score
function showScoreBox() {
var scrollDepth = $(window).scrollTop();
var divPosition = $(".quiz__header").offset().top - 45;
var windowWidth = $(window).width();
// console.log(windowWidth);
if (scrollDepth > divPosition && (windowWidth > 768)) {
$(".quiz__score").show();
$(".quiz__score--mobile").hide();
} else {
$(".quiz__score").hide();
$(".quiz__score--mobile").show();
}
} showScoreBox();
$(window).on("scroll", function(){
showScoreBox();
});
$(window).on("resize", function(){
showScoreBox();
});
var score = 0;
$(document).on("click", ".quiz__response", function(){
$(this).siblings().addBack().addClass("is--unclickable");
$(this).siblings().show("quiz__info"); // Show extra info
console.log("Clicked");
if ($(this).hasClass("answer--true")) {
$(this).addClass("is--true");
$(this).find("i").show();
$(this).siblings().find("i").show();
// Update score
score++
console.log(score);
$(".quiz__correct").html(score);
$(".quiz__correct--mobile").html(score);
rainConfetti();
} else {
// $(this).addClass("is--true");
// $(this).siblings().addClass("is--false");
// $("quiz__info").removeClass("is--false");
$(this).addClass("is--false");
$(this).find("i").show();
$(this).siblings().find("i").show();
}
});
/*-------------------------------------
RESET
--------------------------------------*/
function resetQuiz() {
var score = 0;
$(".quiz__response").removeClass("is--true is--false");
$(".quiz__response").removeClass("is--unclickable");
$(".fa-check").hide();
$(".fa-times").hide();
$(".quiz__correct").html(score);
$(".quiz__correct--mobile").html(score);
}
$(".button__reset").on("click", function(){
var score = 0;
$("canvas").hide();
resetQuiz();
});
【问题讨论】:
标签: javascript jquery function counter reset