【发布时间】:2017-07-19 12:55:07
【问题描述】:
我有一个彩票号码生成器,可以让你生成 6 个介于 1 到 49 之间的随机数。
如何根据生成时包含<p>标签值/数字的div来更改它的背景颜色?
到目前为止,我已经附上了彩票生成器的代码。我只需要根据数字结果更改红色背景颜色,如下所示:
- 1-9:白色
- 10-19:蓝色
- 20-29:平
- 30-39:绿色
- 40-49:黄色
这是我目前用于数字生成的 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