单击 txtAnswerB div 时,我无法更改选择变量的值,这是我的代码:
function checkAnswer(){
var choice;
$("#txtAnswerB").click(function() {
choice = Option2;
$(this).css("background-color","#fff000");
});
}
ps:option2是全局变量
单击 txtAnswerB div 时,我无法更改选择变量的值,这是我的代码:
function checkAnswer(){
var choice;
$("#txtAnswerB").click(function() {
choice = Option2;
$(this).css("background-color","#fff000");
});
}
ps:option2是全局变量
您需要调用该函数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");
});
}
观看现场演示
应该是这样的
<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>