【问题标题】:Targeting group of buttons in quiz, rather than all quiz questions on click针对测验中的一组按钮,而不是点击所有测验问题
【发布时间】:2016-04-06 05:44:59
【问题描述】:

更新 #1


  • 我更新了index.htmlscripts.js

遗留问题

如果我点击了错误的答案……

  • 它不会为刚刚单击的按钮提供类
  • 它给is-falseclass 以正确答案和quiz__info

如果我点击正确答案……

  • 它不会给 is-false 类提供错误答案

scripts.js(新)

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++
        $(".quiz__correct").html(score);
        $(".quiz__correct--mobile").html(score);
    } else {
        // $(this).addClass("is--false");
        $(this).siblings().addClass("is--false");
        $(this).find("i").show();
        $(this).siblings().find("i").show();
    }
});

index.html(每个问题都有这种结构)

        <div class="quiz__question question-5 question__sports">
            <h3>Question #5</h3>
            <p class="quiz__description">A former PGA Tour winner from Brandon passed away at the age of 63. Can you name him?</p>
            <div class="quiz__responses">
                <button class="quiz__response response-5 answer--false">Billy Casper <i class="fa fa-times"></i></button>
                <button class="quiz__response response-5 answer--false">Rory Woods <i class="fa fa-times"></i></button>
                <button class="quiz__response response-5 answer--true">Dan Halldorson <i class="fa fa-check"></i></button>
                <button class="quiz__response response-5 answer--false">Billy Hurley <i class="fa fa-times"></i></button>

                <div class="quiz__info">
                    <p><span class="answer">A: </span>Halldorson competed for nearly 30 years on the PGA Tour, winning the 1980 Pensacola Open. He also captured the 1986 Deposit Guaranty Golf Classic, which was an unofficial tour stop. All told, he had 28 top 10 finishes in 431 PGA Tour events, pocketing nearly $1.2 million in career earnings.</p>
                </div>
            </div> <!-- .responses -->
        </div> <!-- .question -->

原始问题


目标

我有一个有 25 个问题的测验,每个问题都有 4 个可能的答案.quiz__response。单击按钮时,我希望执行以下操作,但仅针对与该特定问题相关的答案,现在它正在将以下内容应用于所有问题。

点击按钮时...

  • addClass(".is--unclickable")这四个答案
  • .show() .fa-check 如果答案是真的,.fa-times 如果答案是假的
  • 如果答案正确,addClass(".is--true") 将背景更改为绿色,addClass(".is--false") 将其余三个答案的背景更改为红色

JSFiddle:https://jsfiddle.net/ub0c6acv/

scripts.js

$(function(){

    /*-------------------------------------
    QUIZ
    --------------------------------------*/

    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;

    $(".quiz__response").on("click", function(){
        $(".quiz__response").addClass("is--unclickable");
        $(".quiz__info").show("quiz__info");  // Show extra info
        console.log("Clicked");

        if ($(this).hasClass("answer--true")) {
            $(this).addClass("is--true");
            $(".fa-check").show();

            // Update score
            score++
            $(".quiz__correct").html(score);
            $(".quiz__correct--mobile").html(score);
        } else {
            $(this).addClass("is--false");
            $(".fa-times").show();
        }
    });

    /*-------------------------------------
    RESET
    --------------------------------------*/

    function resetQuiz() {
      $(".quiz__response").removeClass("answer--true answer--false");
      var score = 0
      $(".quiz__correct").html(score);
      $(".quiz__correct--mobile").html(score);
    }

    $(".button__reset").on("click", function(){
     resetQuiz();
    });

    /*-------------------------------------
    CONFETTI
    --------------------------------------*/

    function rainConfetti() {

        if (score === 25) {
            canvas.show()
        }
    }

    /*-------------------------------------
    SMOOTH SCROLL
    --------------------------------------*/

    $('a[href*=#]:not([href=#])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
            if (target.length) {
                $('html,body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
});

index.html(测验 sn-p)

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

  <div class="quiz">
            <div class="quiz__score">
                <p><span class="quiz__correct">0</span> / 25</p>
            </div>

            <h2 class="quiz__header" id="top">Take the quiz</h2>
            <p class="credit">How well have you been paying attention to what's been happening in southwestern Manitoba in 2015? Take our 25-question quiz to test your local news knowledge.</p>

            <div class="quiz__questions">
            <!-- <canvas id="confetti"></canvas> -->
                <!-- Question -->
                <div class="quiz__question question-1 question__sports">
                    <div class="wrapper">
                        <!-- <img src="http://placehold.it/500x300" alt="" class="quiz__image"> -->
                    </div>
                    <h3>Question #1</h3>
                    <p class="quiz__description">What Brandon Wheat King, who was drafted by the New York Islanders, lost his desire to play hockey and walked away from the team prior to this season starting?</p>
                    <div class="quiz__responses">
                        <button class="quiz__response answer--false">Kale Clague <i class="fa fa-times"></i></button>
                        <button class="quiz__response answer--false">Macoy Erkamps <i class="fa fa-times"></i></button>
                        <button class="quiz__response answer--true">Ryan Pilon <i class="fa fa-check"></i></button>
                        <button class="quiz__response answer--false">Colin Cloutier <i class="fa fa-times"></i></button>


                        <!-- The six­-foot-­two, 212­pound rearguard had 11 goals and 41 assists in 68 regular­ season games, with a goal and 11 assists in 19 playoff contests last season, paired mainly with Russian Ivan Provorov. In 193 regular­season games with Brandon and the Lethbridge,Hurricanes, Pilon has 23 goals, 93 assists and 116 penalty minutes. -->

                    </div> <!-- .responses -->
                </div> <!-- .question -->

                <!-- Question -->
                <div class="quiz__question question-2 question__sports">
                    <div class="wrapper">
                        <!-- <img src="http://placehold.it/500x300" alt="" class="quiz__image"> -->
                    </div>
                    <h3>Question #2</h3>
                    <p class="quiz__description">Which former Brandon skip refused to talk to the local media after losing his fifth provincial men’s curling championship game in six years at Westman Place?</p>
                    <div class="quiz__responses">
                        <button class="quiz__response answer--false">Russ Howard <i class="fa fa-times"></i></button>
                        <button class="quiz__response answer--false">Kerry Burtnyk <i class="fa fa-times"></i></button>
                        <button class="quiz__response answer--true">Mike McEwen <i class="fa fa-check"></i></button>
                        <button class="quiz__response answer--false">Braden Calvert <i class="fa fa-times"></i></button>
                    </div> <!-- .responses -->
                </div> <!-- .question -->

                <!-- McEwen’s Winnipeg based team of B.J. Neufeld, Matt Wozniak and Denni Neufeld lost to Ried Carruthers 5­3 at the Safeway Championship hosted by Brandon. -->
        </div> <!-- quiz -->

【问题讨论】:

    标签: javascript jquery json if-statement for-loop


    【解决方案1】:

    首先,您可以使用 event delegation [部分 直接和委托事件] 绑定单个事件处理程序,而不是为每个按钮绑定一个。

    $(".quiz__response").on("click", function(){
    $(document).on("click", ".quiz__response", function(){

    要仅在与特定问题相关的答案中添加 .is--unclickable,请更改 $(".quiz__response").addClass("is--unclickable");

    $(this).siblings().addBack().addClass("is--unclickable");

    $(this).parent().children().addClass("is--unclickable");

    要显示fa-timesfa-check,请更改
    $(".fa-check").show();
    $(".fa-times").show();
    $(this).find("i").show();在 if 和 else 语句中

    并在您的 if 语句中添加$(this).siblings().addClass("is--false");,如果选中正确答案,则将剩余答案涂成红色。

    编辑

    完整的 js 变化

      $(document).on("click", ".quiz__response", function() {
        $(this).siblings("button").addBack().addClass("is--unclickable");
        $(this).siblings(".quiz__info").show(); // Show extra info
        console.log("Clicked");
    
        if ($(this).hasClass("answer--true")) {
          $(this).addClass("is--true");
          $(this).siblings("button").addBack().find("i").show();
          $(this).siblings("button").addClass("is--false");
    
          // Update score
          score++
          $(".quiz__correct").html(score);
          $(".quiz__correct--mobile").html(score);
        } else {
          $(this).addClass("is--false");
          $(this).find("i").show();
        }
      });
    

    【讨论】:

    • 只需要对我看到的一个问题进行小调整。当我单击不正确的答案时,它不会将is--false 添加到我刚刚单击的quiz__response 中,而是将此类添加到正确答案和div quiz__info 中。我已经更新了上面的scripts.jsindex.html,其中包括您的建议。
    • 仍有一些问题,我已经编辑了上面的问题。
    猜你喜欢
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2020-05-25
    • 2019-11-07
    • 2017-09-07
    • 2021-01-24
    • 1970-01-01
    • 2011-06-22
    相关资源
    最近更新 更多