Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试将随机颜色脚本同步为不同div标记的相同随机颜色_Javascript_Html_Random_Colors_Sync - Fatal编程技术网

Javascript 尝试将随机颜色脚本同步为不同div标记的相同随机颜色

Javascript 尝试将随机颜色脚本同步为不同div标记的相同随机颜色,javascript,html,random,colors,sync,Javascript,Html,Random,Colors,Sync,目前,我正在使用随机颜色脚本从十六进制颜色列表生成背景色,但我开始将此脚本应用于几个不同的div,并希望同步这些颜色,以便在页面加载时它们都更改为相同的颜色。有什么建议吗 这些div都有不同的id。有一些具有相同的类,但也有一些具有不同的类。不确定这是否限制了我的选择 下面是我当前在每个div标记下面使用的脚本: <script type="text/javascript"> function ran_col() { //function name var color

目前,我正在使用随机颜色脚本从十六进制颜色列表生成背景色,但我开始将此脚本应用于几个不同的div,并希望同步这些颜色,以便在页面加载时它们都更改为相同的颜色。有什么建议吗

这些div都有不同的id。有一些具有相同的类,但也有一些具有不同的类。不确定这是否限制了我的选择

下面是我当前在每个div标记下面使用的脚本:

<script type="text/javascript">
    function ran_col() { //function name
    var color = '#'; // hexadecimal starting symbol
    var letters = ['81ADB5','97C29D','E86A72','F1B36C','A37EA3','277F92']; //Set rotating colors here
    color += letters[Math.floor(Math.random() * letters.length)];
    document.getElementById('index-blog').style.background = color; // Setting the random color on your div element.
    }
    setTimeout(ran_col,1); // this will run the code immediately
</script>
如有任何建议,将不胜感激。先谢谢你

更新:

我尝试将下面的脚本添加到head标记中,它在div id上运行了几次,但在刷新页面几次后,div停止更改颜色并变为空白

  <script type="text/javascript">
    function ran_col() { //function name
    var color = '#'; // hexadecimal starting symbol
    var letters = ['81ADB5','97C29D','E86A72','F1B36C','A37EA3','277F92']; //Set rotating colors here
    color += letters[Math.floor(Math.random() * letters.length)];
    document.getElementById('parallelogram').style.background = color; // Setting the random color on your div element.
    }
    setTimeout(ran_col,1); // this will run the code immediately
</script>
有什么建议吗


我也尝试过使用getElementbyClass,但也不起作用。任何建议都将不胜感激。

每次调用Math.random时,您都会生成一个新值,以便每个div的颜色值都不同。请尝试在文档的标题部分设置颜色值,然后将其应用于div。这样所有的颜色都一样。谢谢你的快速回复@DennisPriebe!你有没有可能给出一个在标题中的代码示例?我对javascript还不熟悉,所以我想尝试发明一种灯泡,让我自己反复尝试。我一直在看其他的帖子和文章,看看是否能找到像这样的东西,但我没有找到我需要学习的东西。