0

我正在做一个测验,这个测验有一个计时器。当计时器结束时,它会触发一个使用 href 作为目标的 scrollLeft 动画。我想要做的是解决它始终以第一个为目标的问题.qAnswers li a是使用变量来更改 href-target 的名称。好吧,它似乎没有按计划工作,我做错了什么?

这是该项目的链接:http: //www.carlpapworth.com/friday-quiz/#

HTML:

<div id="qWrap">
                <ul id="qBox">
<!--Q1-->           <li id="q1" class="qContainer">
                        <div class="qQuestion">Question I                       </div>
                        <ul class="qAnswers">
                            <li><a href="#q2" class="aWrong"><h3>Answer a</h3></a></li>
                            <li><a href="#q2" class="aCorrect"><h3>Answer b</h3></a></li>
                            <li><a href="#q2" class="aWrong"><h3>Answer c</h3></a></li>
                        </ul>
                    </li>
<!--Q2-->           <li id="q2" class="qContainer">
                        <div class="qQuestion">Question II                      </div>
                        <ul class="qAnswers">
                            <li><a href="#q3" class="aWrong"><h3>Answer 1</h3></a></li>
                            <li><a href="#q3" class="aWrong"><h3>Answer 2</h3></a></li>
                            <li><a href="#q3" class="aCorrect"><h3>Answer 3</h3></a></li>
                        </ul>
                    </li>
<!--Q3-->           <li id="q3" class="qContainer">
                        <div class="qQuestion">Question III                     </div>
                        <ul class="qAnswers">
                            <li><a href="#q4" class="aWrong"><h3>Answer 1</h3></a></li>
                            <li><a href="#q4" class="aWrong"><h3>Answer 2</h3></a></li>
                            <li><a href="#q4" class="aCorrect"><h3>Answer 3</h3></a></li>
                        </ul>
                    </li>
</ul>
</div>

JS:

//timer//

function timesUp(){
            $('#timerBar').animate({'width':'0px'}, 7000, function(){
                nextQuestion(function(){
                    resetTimer();
                }); 
            });                 
}

var hrefURL = '#q';
var hrefData = 2; <!-- VARIABLE -->
$('.qAnswers a:first').attr('href', hrefURL + hrefData);

function nextQuestion(event){
    var $anchor = $('.qAnswers a:first');
     $('#qWrap').stop(true, true).animate({
        scrollLeft: $($anchor.attr('href')).position().left
        }, 2000, function(){
            nextCount();  
hrefData++; <!-- INCREASE VARIABLE -->

        });

}
4

1 回答 1

1
FINALY! I found the answer to my own problem: 
    var hrefURL = '#q';
    var hrefData = 2;
    $('.qAnswers a:first').attr('href', hrefURL + hrefData);

    function nextQuestion(event){
        var $anchor = $('.qAnswers a:first');
         $('#qWrap').stop(true, true).animate({
            scrollLeft: $($anchor.attr('href')).position().left
            }, 2000, function(){
                nextCount();  
    hrefData++;
    $('.qAnswers a:first').attr('href', hrefURL + hrefData); <!-- I need to fire this again for the hrefData++; to be applied  -->

            });

}
于 2013-03-12T08:43:37.897 回答