【问题标题】:generate multiple colours with javascript [duplicate]用javascript生成多种颜色[重复]
【发布时间】:2017-10-14 09:22:50
【问题描述】:

我正在做一个小项目,其中使用 javascript 生成和显示多种颜色。 https://mrkwrght.github.io/totallycolours/

我的代码目前是这样的:

 var randomColor15 = "000000".replace(/0/g,function(){return (~~

(Math.random()*16)).toString(16);});

$(function() {

$("#colorbox").css({
    backgroundColor:'#' + randomColor
});
$("#colorcode").text("#" + randomColor);

此块重复 16 次。必须有更简单的方法来执行此操作。

我还计划用加载更多按钮替换刷新按钮。但我不确定如何做到这一点

【问题讨论】:

  • 怎么重复,#colorbox应该只有一个单个元素。无论如何,您只生成一种颜色,并且每次都使用相同的颜色?

标签: javascript


【解决方案1】:

你必须把它放在一个函数中,然后调用

var randomColor15 = function(){return "#"+"000000".replace(/0/g,function(){return (~~(Math.random()*16)).toString(16)})};
//only test
for(var i=0;i<10;i+=1){
  console.log(randomColor15());
}

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 1970-01-01
    • 2021-06-02
    • 2013-07-06
    • 2011-11-27
    • 1970-01-01
    • 2021-07-20
    • 2013-12-31
    • 1970-01-01
    相关资源
    最近更新 更多