【问题标题】:Change background colour based on value output根据值输出更改背景颜色
【发布时间】:2017-07-19 12:55:07
【问题描述】:

我有一个彩票号码生成器,可以让你生成 6 个介于 1 到 49 之间的随机数。

如何根据生成时包含<p>标签值/数字的div来更改它的背景颜色?

到目前为止,我已经附上了彩票生成器的代码。我只需要根据数字结果更改红色背景颜色,如下所示:

  • 1-9:白色
  • 10-19:蓝色
  • 20-29:平
  • 30-39:绿色
  • 40-49:黄色

CodePen Example

这是我目前用于数字生成的 JavaScript:-

$("#buttonGen").click(function() {
  $(".circle .result1").html((Math.floor(Math.random() * 49) + 1));
  $(".circle .result2").html((Math.floor(Math.random() * 49) + 1));
  $(".circle .result3").html((Math.floor(Math.random() * 49) + 1));
  $(".circle .result4").html((Math.floor(Math.random() * 49) + 1));
  $(".circle .result5").html((Math.floor(Math.random() * 49) + 1));
  $(".circle .result6").html((Math.floor(Math.random() * 49) + 1));
});

【问题讨论】:

  • 到目前为止你尝试过什么?您在哪里尝试更改背景或检查值?
  • 到目前为止我还没有尝试过:(
  • 所以生成数字,添加if语句获取颜色,设置背景颜色,设置文本。
  • @CharlieMcShane 那我建议你试试看?
  • @CharlieMcShane 试试codepen.io/anon/pen/YQoYpe

标签: javascript jquery css


【解决方案1】:

要执行您需要的操作,您可以首先将相同的类应用于所有 .resultN 元素。这将允许您使用对 html() 的单个调用来影响所有元素,从而使您的代码干燥。

然后您可以设置它们的随机值,还可以使用switch 语句根据该值更改它们的背景颜色。试试这个:

$("#buttonGen").click(function() {
  $(".circle .result").text(function() {
    var value = parseInt(Math.floor(Math.random() * 49) + 1), bgColour;
    switch (true) {
      case (value < 10): bgColour = 'white'; break;
      case (value < 20): bgColour = 'blue'; break;
      case (value < 30): bgColour = 'pink'; break;
      case (value < 40): bgColour = 'green'; break;
      case (value < 50): bgColour = 'yellow'; break;
    }   
    $(this).closest('.circle').css('background-color', bgColour);
    return value;
  });
});
.__hero-container .title {
  margin-bottom: 30px;
}

.__hero-container .numbers .circle {
  background-color: red;
  border-radius: 50px;
  height: 90px;
}

.__hero-container .numbers .circle p {
  position: relative;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}

.__hero-container .button {
  margin-top: 30px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="__hero-container">

  <div class="container title">
    <div class="col-md-12 text-center">
      <h1>Lottery Generator</h1>
    </div>
  </div>

  <div class="container text-center numbers">
    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result">0</p>
      </div>
    </div>
  </div>

  <div class="container button">
    <div class="col-md-12 text-center">
      <button id="buttonGen">Generate</button>
    </div>
  </div>

</div>

【讨论】:

  • 这个是最佳答案。+1.deleted my-one
  • 哇,非常好的方法。感谢您为我分解它,这非常有效。再次感谢!
  • 没问题,很乐意提供帮助。
【解决方案2】:

您可以为此使用.each()。检查下面更新的 sn-p..

$("#buttonGen").click(function() {
  $(".circle .result1").html((Math.floor(Math.random() * 49) + 1));
    $(".circle .result2").html((Math.floor(Math.random() * 49) + 1));
    $(".circle .result3").html((Math.floor(Math.random() * 49) + 1));
    $(".circle .result4").html((Math.floor(Math.random() * 49) + 1));
    $(".circle .result5").html((Math.floor(Math.random() * 49) + 1));
    $(".circle .result6").html((Math.floor(Math.random() * 49) + 1));

    $('.circle').each(function(){
         var numResult = parseInt($(this).find('p').html());
         if(numResult >=1 && numResult <=9) {
             $(this).css('background','white');
         } else if (numResult >=10 && numResult <=19) {
             $(this).css('background','blue');
         } else if (numResult >=20 && numResult <=29) {
             $(this).css('background','pink');
         } else if (numResult >=30 && numResult <=39) {
             $(this).css('background','green');
         } else if (numResult >=40 && numResult <=49) {
             $(this).css('background','yellow');
         }
         
         
    })

})
.__hero-container .title {
  margin-bottom: 30px;
}

.__hero-container .numbers .circle {
  background-color: red;
  border-radius: 50px;
  height: 90px;
  border: 1px solid #000; 
}

.__hero-container .numbers .circle p {
  position: relative;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}

.__hero-container .button {
  margin-top: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="__hero-container">

  <div class="container title">
    <div class="col-md-12 text-center">
      <h1>Lottery Generator</h1>
    </div>
  </div>
  
  <div class="container text-center numbers">
    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result1">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result2">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result3">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result4">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result5">0</p>
      </div>
    </div>

    <div class="col-md-2 col-xs-2 text-center">
      <div class="circle">
        <p class="result6">0</p>
      </div>
    </div>
  </div>
  
  <div class="container button">
    <div class="col-md-12 text-center">
      <button id="buttonGen">Generate</button>
    </div>
  </div>
  
</div>

【讨论】:

    【解决方案3】:

    只需为检查内容的所有元素添加一个循环,然后添加特定背景

    $( ".circle p" ).each(function(index) {
        if ($(this).html()>0 && $(this).html()<10){
          $(this).parent().css("background","black");
        }else if ($(this).html()>9 && $(this).html()<20){
          $(this).parent().css("background","blue");
        }
        //continue the rest for Ping, Green and Yellow
      });
    

    【讨论】:

    • 非常好的方法。奇迹般有效。谢谢! :)
    【解决方案4】:

    这将对您有所帮助,只需添加其他验证...

    $("#buttonGen").click(function() {
      $(".result1").html((Math.floor(Math.random() * 49) + 1));
      $(".result2").html((Math.floor(Math.random() * 49) + 1));
      $(".result3").html((Math.floor(Math.random() * 49) + 1));
      verifyContent();
    });
    
    function verifyContent() {
      $('.circle').each(function(i, obj) {
        var content = $(this).html();
        //console.log(content);
        if (content > 10 && content < 19) {
          $(this).css("background", "blue");
        } else {
          $(this).css("background", "white");
        }
      });
    }
    .circle {
      height: 150px;
      width: 150px;
      border: 1px solid black;
      border-radius: 50%;
      display: inline-block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button id="buttonGen">Generate</button><br><br>
    <div class="circle result1"></div>
    <div class="circle result2"></div>
    <div class="circle result3"></div>

    【讨论】:

    • 这是一个非常简单直接的方法。好东西,谢谢! :)
    【解决方案5】:

    在 html 设置之后,我可能会有一个函数来根据它确定颜色。

    $("#buttonGen").click(function() {
      $(".circle .result1").html((Math.floor(Math.random() * 49) + 1));
      $(".circle .result2").html((Math.floor(Math.random() * 49) + 1));
      $(".circle .result3").html((Math.floor(Math.random() * 49) + 1));
      $(".circle .result4").html((Math.floor(Math.random() * 49) + 1));
      $(".circle .result5").html((Math.floor(Math.random() * 49) + 1));
      $(".circle .result6").html((Math.floor(Math.random() * 49) + 1));
      determineColor();
    });
    
    function determineColor(){
     $(".circle").each(function(index, value){
        var color = getColorValue(value);
        $(this).css("background-color", color) 
     }
    }
    
    function getColorValue(value){
      if(value > 1 && value < 9){
        return "white";
      }
      // add others
    }
    

    【讨论】:

    • 非常好的布局。易于阅读代码的工作原理。非常感谢您的帮助! :)
    猜你喜欢
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 2016-06-02
    • 2019-06-20
    • 1970-01-01
    • 2021-08-29
    • 2013-09-03
    • 2020-10-11
    相关资源
    最近更新 更多