Javascript测验没有过渡

时间:2015-09-30 18:38:56

标签: javascript

我正在尝试进行测验,我一次显示一个问题,当你回答它时,你会转到下一个问题。我有一些像这样的基本标记

<div id="quizzie">
    <div class="col-md-12 quiz-container current">
        <div class="col-md-6">
            <img src="../images/image.jpg">
        </div>
        <div class="col-md-6 extra">
            <ul class="quiz-step step1">
                <li class="question">
                    <div class="question-wrap">
                        <h3 class="question-title">Some Question:</h3>
                    </div>
                </li>
                <li class="quiz-answer low-value" data-quizIndex="2" data-number="1">
                    <div class="col-md-12 add-border add-spacing">
                        <div class="answer-wrap">
                            <p class="answer-text">This Thing</p>
                        </div>
                    </div>
                </li>
                <li class="quiz-answer high-value" data-quizIndex="4" data-number="1">
                    <div class="col-md-12 add-border add-spacing">
                        <div class="answer-wrap">
                            <p class="answer-text">That Thing</p>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <div class="col-md-12 quiz-container">
        <div class="col-md-6">
            <img src="../images/image.jpg">
        </div>
        <div class="col-md-6 extra">
            <ul class="quiz-step step2">
                <li class="question">
                    <div class="question-wrap">
                        <h3 class="question-title">Some Question:</h3>
                    </div>
                </li>
                <li class="quiz-answer low-value" data-quizIndex="2" data-number="2">
                    <div class="col-md-12 add-border add-spacing">
                        <div class="answer-wrap">
                            <p class="answer-text">This Thing</p>
                        </div>
                    </div>
                </li>
                <li class="quiz-answer high-value" data-quizIndex="4" data-number="2">
                    <div class="col-md-12 add-border add-spacing">
                        <div class="answer-wrap">
                            <p class="answer-text">That Thing</p>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</div>

然后我确保显示第一个问题,然后隐藏下一个问题

#quizzie .quiz-container {
    display: none;
    padding:0;
}
#quizzie .current {
    display: block;
}

现在我的Javascript代码就像这样

var quizSteps = $('#quizzie .quiz-step');

quizSteps.each(function () {
    var currentStep = $(this),
        ansOpts = currentStep.children('.quiz-answer');

    ansOpts.each(function () {

        var eachOpt = $(this);
        eachOpt[0].addEventListener('click', check, false);
        function check() {
            var $this = $(this),
                value = $this.attr('data-quizIndex'),
                answerScore = parseInt(value);

            if (currentStep.children('.active').length > 0) {
                var wasActive = currentStep.children('.active'),
                    oldScoreValue = wasActive.attr('data-quizIndex'),
                    oldScore = parseInt(oldScoreValue);
                currentStep.children('.active').removeClass('active');
                $this.addClass('active');

            } else {
                $this.addClass('active');
                updateStep(currentStep);
            }
        }
    });
});

function updateStep(currentStep) {
    if(currentStep.hasClass('current')){
        currentStep.removeClass('current');
        currentStep.next().addClass('current');
    }
}

现在它似乎正确设置了活动类,但它没有正确设置当前类,这意味着我总是被问到第一个问题。我怎样才能让它进入下一个问题呢?

由于

1 个答案:

答案 0 :(得分:1)

你应该在&#34; .quiz-container&#34;上切换当前课程。元素不在&#34; .quiz-step&#34;元件。

Js fiddle