只是为了好玩,我想让我的网站具有动态背景颜色,它是基于当前时间的 rgb 值。我进行了一些谷歌搜索并得出以下结果,但它不起作用。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Site</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link type="text/javascript" src="date.js">
<style>
p{
font-size: 150%;
}
</style>
</head>
<body>
</body>
</html>
日期.js
$(document).ready(function() {
setInterval(function(){currentTime("#idTimeField")}, 500);
});
function currentTime(field) {
var now = new Date();
now = (now.getHours() * 10) + ', ' + (now.getMinutes() * 4) + ', ' + (now.getSeconds() * 4);
$(field).val(now);
}
document.getElementsByName("html").style.backgroundColor = "rgb(" + now + ")";
任何人都可以帮助我解决我做错的事情,或者其他/更好的方法吗? 如果我做了一些愚蠢的事情,我深表歉意,我是一个 javascript 菜鸟
提前致谢
最佳答案
这里。所有 jQuery。代码修改后可以工作。
$(document).ready(function() {
setInterval(function(){currentTime("#idTimeField")}, 500);
});
function currentTime(field) {
var now = new Date();
now = (now.getHours() * 10) + ', ' + (now.getMinutes() * 4) + ', ' + (now.getSeconds() * 4);
$(field).val(now);
$('html,body').css({'background-color':'rgb('+now+')'});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Site</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link type="text/javascript" src="date.js">
<style>
p{
font-size: 150%;
}
</style>
</head>
<body>
<div id="idTimeField">20:10:34</div>
</body>
</html>
关于javascript - 使用时间作为 css 背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41053084/