通过递增变量来改变href-target

时间:2013-03-11 08:07:57

标签: javascript jquery variables href

我正在进行测验,这个测验有一个计时器。当计时器结束时,它会触发一个使用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 -->

        });

}

1 个答案:

答案 0 :(得分: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  -->

            });

}