【发布时间】:2018-08-27 02:58:48
【问题描述】:
我在实验的特定部分有四个按钮,这是HTML代码。
<div id="test" style="display:none">
<div class="row center-block text-center">
<img id="testImage" src="static/img/errorhedgehog.jpg" height="300">
</div>
<div class="row text-center center-block">
<h3 id="labelPrompt">Choose the corresponding label:</h3>
</div>
<div class="row text-center center-block">
<button class="quizChoice btn btn-md btn-info" id="quizChoice0"></button>
</div>
<div class="row text-center center-block">
<button class="quizChoice btn btn-md btn-info" id="quizChoice1"></button>
</div>
<div class="row text-center center-block">
<button class="quizChoice btn btn-md btn-info" id="quizChoice2"></button>
</div>
<div class="row text-center center-block">
<button class="quizChoice btn btn-md btn-info" id="quizChoice3"></button>
</div>
<div class="row text-center">
<p>Press enter or click 'Next' to go to the next item.</p>
<button id="nextTest" class="btn btn-sm btn-success">Next</button>
</div>
<div class="progress">
<div id="testProgress" class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
四个不同的答案出现在按钮上。正确的一个在脚本中定义,其他三个出现在其他按钮上。
var rightLabel = partTest[trial].label
var choiceLabels = ["hola", "hole", "holo", rightLabel];
$("choiceLabels").css("background-color", "transparent");
for (var i=0; i<choiceLabels.length; i++) {
var theID="#quizChoice"+i.toString();
$(theID).text(choiceLabels[i])
我正在尝试更改按钮的颜色,以便在单击正确答案时,此按钮的背景颜色变为绿色。同样,当点击错误答案时,该按钮的背景色变为红色,右侧变为绿色。
$(".quizChoice").click(function() {
var theChoice=$(this).text();
if (theChoice===rightLabel) {
alert("Good job!")
$("#rightLabel").css("background-color", "green");
} else {
alert("Too bad! The correct label is " + rightLabel);
$("#theChoice").css("background-color", "red");
$("#rightLabel").css("background-color", "green");
}
});
但什么都没有改变?
【问题讨论】:
-
您收到哪个警报?
-
根据我单击的按钮,会出现警报(“干得好!”或“太糟糕了!”),但颜色不会改变。
-
我确实将您的代码复制到了代码笔中,但没有任何效果
-
这是整个实验的一部分,包含不同的训练和测试轮次,但我只是复制了我认为最相关的部分。
-
我在 HTML 中看不到
#theChoice和#rightLabel。他们在哪里?
标签: javascript jquery css button background-color