gpt4 book ai didi

javascript - 随机颜色字符串

转载 作者:行者123 更新时间:2023-12-02 17:33:01 24 4
gpt4 key购买 nike

首先,这不是 Generate random string/characters in JavaScript 的重复项

我特别想要一个字符串更改为从绿色开始的随机颜色。我是用JS写的

function changeColor()
{
var change;
for( var i=0; i < 11; i++ )
{
change += script.charAt(Math.floor(Math.random() //Input ur suggestion here, I suspect it is the script variable and different colours
script = document.getElementById('txt');
script.style.color = "#33cc33";
}
}

最佳答案

for 循环立即完成,您需要一个超时才能实际看到颜色变化,需要一个随机颜色生成器来生成颜色,然后放入递归 IIFE 就可以了:

var color = '#33cc33',
elem = document.getElementById('txt');

(function fn() {
elem.style.color = color;
setTimeout(function() {
color = '#'+Math.floor(Math.random()*16777215).toString(16);
fn();
}, 500);
})();

FIDDLE

关于javascript - 随机颜色字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22885956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com