用js写个时钟并且时钟的字体颜色随时间变化
1个回答
展开全部
<html>
<script>
window.onload = function() {
var clock = document.getElementById('clock');
var color = ['red', 'green', 'yellow', 'blue', 'black', 'gold', 'orange', 'gray', 'pink', 'maroon']; //钟表颜色数组
setInterval(function() {
var now = new Date();
clock.innerHTML = now.getFullYear() + '年' + (now.getMonth()+1) + '月' + now.getDate() + '日' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
var radom = Math.floor(Math.random() * color.length + 1) - 1; //随机数
clock.style.color = color[radom]; //设置随机颜色
}, 1000)
}
</script>
<body>
<span id="clock" style="font-size:20px;"></span>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询