Javascript 是否可以根据时间更改网页的背景颜色?
Javascript 是否可以根据时间更改网页的背景颜色?,javascript,html,css,bootstrap-4,Javascript,Html,Css,Bootstrap 4,函数init(){ 函数setBackgroundForTimeOfDay(){ const body=document.querySelector('body'); const hours=新日期().getHours(); 如果(9您必须对您的情况执行以下操作: function init() { function setBackgroundForTimeOfDay() { const body = document.querySelector('body'); con
函数init(){
函数setBackgroundForTimeOfDay(){
const body=document.querySelector('body');
const hours=新日期().getHours();
如果(9您必须对您的情况执行以下操作:
function init() {
function setBackgroundForTimeOfDay() {
const body = document.querySelector('body');
const hours = new Date().getHours();
if (9 <= hours && hours <= 12) {
body.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 231, 255),rgb(39, 38, 38))';
} else if (hours > 12 && hours <= 15) {
body.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
} else {
body.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
}
}
}
function init1() {
function setBackgroundForTimeOfDay() { // <-- Why do these functions have the same name
const body = document.querySelector('body');
const hours = new Date().getHours();
if (9 <= hours && hours <= 12) {
ul.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 231, 255),rgb(39, 38, 38))';
} else if (hours > 12 && hours <= 15) {
ul.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
} else {
ul.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
}
}
setBackgroundForTimeOfDay();
setInterval(setBackgroundForTimeOfDay, 60000);
}
函数init(){
函数setBackgroundForTimeOfDay(){
const body=document.querySelector('body');
const hours=新日期().getHours();
如果(9您必须对您的情况执行以下操作:
function init() {
function setBackgroundForTimeOfDay() {
const body = document.querySelector('body');
const hours = new Date().getHours();
if (9 <= hours && hours <= 12) {
body.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 231, 255),rgb(39, 38, 38))';
} else if (hours > 12 && hours <= 15) {
body.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
} else {
body.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
}
}
}
function init1() {
function setBackgroundForTimeOfDay() { // <-- Why do these functions have the same name
const body = document.querySelector('body');
const hours = new Date().getHours();
if (9 <= hours && hours <= 12) {
ul.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 231, 255),rgb(39, 38, 38))';
} else if (hours > 12 && hours <= 15) {
ul.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
} else {
ul.style['background-color'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
}
}
setBackgroundForTimeOfDay();
setInterval(setBackgroundForTimeOfDay, 60000);
}
函数init(){
函数setBackgroundForTimeOfDay(){
const body=document.querySelector('body');
const hours=新日期().getHours();
如果(9我在脚本中做了一些更改
我已经从您的HTML在加载javascript之前加载的dom(HTML)中删除了所有onload调用,因此最初这些函数不可用
在菜单上添加了id=“导航菜单”
并在下面的代码中进行更改
function init() {
const body = document.querySelector('body');
const hours = new Date().getHours();
const ul = document.getElementById('nav_menu')
if (9 <= hours && hours <= 12) {
body.style['background-image'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 231, 255),rgb(39, 38, 38))';
changeMenuColor('linear-gradient(to right, rgb(39, 38, 38), rgb(96, 231, 255),rgb(39, 38, 38))');
} else if (hours > 12 && hours <= 15) {
body.style['background-image'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
changeMenuColor('linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))');
} else
body.style['background-image'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
changeMenuColor('linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))');
}
function changeMenuColor(color) {
elements = document.getElementsByClassName('navbar-menu');
for (var i = 0; i < elements.length; i++) {
elements[i].querySelector('a').style['background-image'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
}
}
init();
函数init(){
const body=document.querySelector('body');
const hours=新日期().getHours();
const ul=document.getElementById('导航菜单')
如果(9我在脚本中做了一些更改
我已经从您的HTML在加载javascript之前加载的dom(HTML)中删除了所有onload调用,因此最初这些函数不可用
在菜单上添加了id=“导航菜单”
并在下面的代码中进行更改
function init() {
const body = document.querySelector('body');
const hours = new Date().getHours();
const ul = document.getElementById('nav_menu')
if (9 <= hours && hours <= 12) {
body.style['background-image'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 231, 255),rgb(39, 38, 38))';
changeMenuColor('linear-gradient(to right, rgb(39, 38, 38), rgb(96, 231, 255),rgb(39, 38, 38))');
} else if (hours > 12 && hours <= 15) {
body.style['background-image'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
changeMenuColor('linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))');
} else
body.style['background-image'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
changeMenuColor('linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))');
}
function changeMenuColor(color) {
elements = document.getElementsByClassName('navbar-menu');
for (var i = 0; i < elements.length; i++) {
elements[i].querySelector('a').style['background-image'] = 'linear-gradient(to right, rgb(39, 38, 38), rgb(96, 255, 207),rgb(39, 38, 38))';
}
}
init();
函数init(){
const body=document.querySelector('body');
const hours=新日期().getHours();
const ul=document.getElementById('导航菜单')
如果(99哦!我现在就纠正它。为什么我看到这么多人不在他们的小提琴上使用漂亮而有用的[Tidy]按钮?(大声说一句话,不要把它看得太坏。)这并不能解决问题。问题仍然存在。我是一个完全的新手。对于给您带来的不便,我真的很抱歉。9哦!我现在就纠正它。为什么我看到这么多人没有在他们的小提琴上使用漂亮而有用的[整洁]按钮?(大声说一句话,不要太坏。:)它不能解决问题。问题仍然存在。我是一个完全的新手。对于给您带来的不便,我真的很抱歉。问题仍然存在,背景颜色不会随时间而改变。(我正在处理它。我在代码中添加了注释。您能解释为什么函数具有相同的名称吗?一个具有init1(),另一个具有init()。它们都可以根据时间设置导航条和背景的颜色。它们是否相互冲突?因为它们在不同的功能中,我假设它们不会以任何方式冲突。所以我就这样保留了。问题仍然存在,背景颜色不会随时间而变;(我正在研究它。我在代码中添加了一条注释。你能解释一下为什么函数有相同的名称吗?一个有init1(),另一个有init()。它们都可以根据时间设置导航条和背景的颜色。它们是否相互冲突?因为它们在不同的功能中,所以我认为它们不会以任何方式冲突。因此,我保留了这种方式。我更新了代码。它仍然不起作用;(签出并检查我更新的代码中所有的HTML更改。它仍然不起作用。;(签出并检查所有的HTML更改。)