【问题标题】:Changing color of buttons when wrong/right answer is clicked单击错误/正确答案时更改按钮颜色
【发布时间】: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


【解决方案1】:

您将rightLabeltheChoice 变量名称作为字符串的一部分放在jQuery 选择器中,而不是引用该值。根据您的示例尝试这样的操作:

var rightLabel = 'B';

$(".quizChoice").click(function() {
  var theChoice = $(this).text();

  if (theChoice === rightLabel) {
    alert("Good job!")
    $("#" + theChoice).css("background-color", "green");
  } else {
    alert("Too bad! The correct label is " + rightLabel);
    $("#" + theChoice).css("background-color", "red");
    $("#" + rightLabel).css("background-color", "green");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="quizChoice" id="A">A</div>
<div class="quizChoice" id="B">B</div>

【讨论】:

  • $(#${theChoice}) 可以是$('#'+theChoice) 是吗?
  • @CraigvanTonder,不,这些是字符串模板。注意反引号。这有点 ES6-y 但它工作得很好。
  • 它可能是 - 它不会对执行产生影响。我只是更喜欢使用模板文字。但是使用两种样式并不好,所以我更新了答案以匹配问题样式
  • 当然我明白这一点,只是认为将变量连接到字符串时更容易理解。也许对于更大的字符串模板文字会更有意义:)
猜你喜欢
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 2012-12-06
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 2021-07-21
相关资源
最近更新 更多