gpt4 book ai didi

javascript - 条件语句无法正常运行

转载 作者:行者123 更新时间:2023-12-02 21:14:36 25 4
gpt4 key购买 nike

取决于一天中的时间。我正在尝试使用 JavaScript ( new Date() ) 方法向用户显示上午、下午或晚上。但我收到的回应只是(其他)声明。我可以获得关于我做错了什么的建议吗?

const mainHeader = document.getElementById("header");

var greetings = () => {
var today = new Date();
}

window.onload= (e) => {
greetings;


if(today >= 1 && today < 12) {
mainHeader.innerHTML = "<h3>Good Morning! And Welcome To Guess The Number</h3>"
console.log("Morning: between 1 and 12");
} else if(today >= 12 && today < 17) {
mainHeader.innerHTML = "<h3>Good Afternoon! And Welcome To Guess The Number</h3>"
console.log("Afternoon: between 12 and 17");
} else if(today >= 17 && today <= 24) {
mainHeader.innerHTML = "<h3>Good Evening! And Welcome To Guess The Number</h3>"
console.log("Evening: between 17 and 24");
} else {
mainHeader.innerHTML = "<h3>Welcome to Guess The Number</h3>"
console.log("No time zone");
}
}

最佳答案

您需要从 greetings() 返回小时,然后将其分配给 window.onload 函数中的变量。

const mainHeader = document.getElementById("header");

var greetings = () => {
var today = new Date();
return today.getHours();
}

window.onload = (e) => {
var today = greetings()
if (today >= 1 && today < 12) {
mainHeader.innerHTML = "<h3>Good Morning! And Welcome To Guess The Number</h3>"
console.log("Morning: between 1 and 12");
} else if (today >= 12 && today < 17) {
mainHeader.innerHTML = "<h3>Good Afternoon! And Welcome To Guess The Number</h3>"
console.log("Afternoon: between 12 and 17");
} else if (today >= 17 && today <= 24) {
mainHeader.innerHTML = "<h3>Good Evening! And Welcome To Guess The Number</h3>"
console.log("Evening: between 17 and 24");
} else {
mainHeader.innerHTML = "<h3>Welcome to Guess The Number</h3>"
console.log("No time zone");
}
}
<div id="header">
</div>

关于javascript - 条件语句无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61003217/

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