-2

单击 txtAnswerB div 时,我无法更改选择变量的值,这是我的代码:

function checkAnswer(){
    var choice;

    $("#txtAnswerB").click(function() {
        choice = Option2;
        $(this).css("background-color","#fff000");
    });
}

ps:option2是全局变量

4

2 回答 2

0

您需要调用该函数checkAnswer();才能click()初始化事件...

例子:

HTML:

<div id="answerB" class="answer"> <div id="txtAnswerB" class="txtAnswer">Quest</div> </div>​

jQuery:

checkAnswer();
function checkAnswer(){
var choice;
    $("#txtAnswerB").click(function() {
        choice = Option2;
        $(this).css("background-color","red");
    });
}​

观看现场演示

于 2012-11-24T21:21:24.840 回答
0

应该是这样的

    <script type="text/javascript">
        var choice;
        var Option2 = /*somenthing*/;

        $(function(){
        $("#txtAnswerB").bind('click',function(){
             checkAnswer($(this));
           });
        });

        function checkAnswer(_element){
        choice = Option2;
        $(_element).css("background-color","#fff000");
        alert(choice);

        }
    </script>
于 2012-11-24T21:10:34.873 回答