单击每次按钮时随机颜色

时间:2016-04-01 21:48:13

标签: javascript jquery

我希望每次单击按钮时,div都会更改为makeColor变量生成的随机颜色。提前谢谢。

var makeColor ="#" + Math.floor((Math.random() * 999) + 1);

$("button").click(function(){
  $("div").css("background",makeColor);
});

1 个答案:

答案 0 :(得分:2)

使用此功能执行此任务



function randomColor() {
    var c = "#";
    for (var i = 0; i < 6; i++) {
        c += (Math.random() * 16 | 0).toString(16);
    } 
    return c;
}

var a = document.getElementById("id1").style;
a.color = randomColor();
&#13;
<h1 id="id1">stackoverflow</h1>
&#13;
&#13;
&#13;

相关问题