Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Javascript显示问候语_Javascript - Fatal编程技术网

用Javascript显示问候语

用Javascript显示问候语,javascript,Javascript,如何在javascript中根据一天中的时间显示问候语 var greetdate=new Date() var hours=greetdate.getHours() document.write('<h2>Hello! Good Morning! Have a nice day.</h2>') document.write('<h2>Good Noon Visitor! </h2>') //MESSAGE FOR AFTERNOON doc

如何在javascript中根据一天中的时间显示问候语

var greetdate=new Date()
var hours=greetdate.getHours()

document.write('<h2>Hello! Good Morning! Have a nice day.</h2>')

document.write('<h2>Good Noon Visitor! </h2>')

//MESSAGE FOR AFTERNOON
document.write('<h2>Good Afternoon! </h2>')

//MESSAGE FOR EVENING (6pm-8pm)
document.write('<h2>Good Evening! </h2>')

//MESSAGE FOR NIGHT (9pm-11pm)
document.write('<h2>Good Night! </h2>')

//MESSAGE FOR LATE NIGHT (12pm-4am)
document.write('<h2>Wow! You`re still awake. Working Late?</h2>')
var greetdate=新日期 var hours=greetdate.getHours
如果小时数>=5&&hours=13&&hours=18&&hours=21&&hours切换的良好用例:

var h=(new Date()).getHours();
switch(true){
case h<13 :
  document.write('<h2>Hello! Good Morning! Have a nice day.</h2>');
break;
case h<=14 :
    document.write('<h2>Good Noon Visitor! </h2>')
break;
case h<18 :
    document.write('<h2>Good Afternoon! </h2>')
break;
case h<21 :
    document.write('<h2>Good Evening! </h2>')
break;
case h<24 :
    document.write('<h2>Good Night! </h2>');
break;
}
所有输出:

var now=新日期; var hours=now.getHours; var问候语=[ {托德:深夜, 哇!你还没睡.工作到很晚?, 测试:小时数>=24 | |小时数=21 | |小时数=18&&hours=13&&hours=5&&hours el.test.greet; message.className=问候语.findel=>el.test.tod; 深夜{ 颜色:紫色; 背景:ccf; } h2.晚上{ 颜色:银色; 背景:2000人; } 下午2:00{ 颜色:海军蓝; 背景:eee; } 下午2:00{ 颜色:红色; 背景:ffdede; } 下午2:00{ 颜色:青色; 背景:一枝黄花; } 早上好{ 颜色:蓝色; 背景:ffe; }
如果问候语是一个数组,那么您可以使用mess.innerHTML=greetings.findel=>el.test.greeting;它更简单一点;谢谢!非常好的建议-非常有用。
var h=(new Date()).getHours();
switch(true){
case h<13 :
  document.write('<h2>Hello! Good Morning! Have a nice day.</h2>');
break;
case h<=14 :
    document.write('<h2>Good Noon Visitor! </h2>')
break;
case h<18 :
    document.write('<h2>Good Afternoon! </h2>')
break;
case h<21 :
    document.write('<h2>Good Evening! </h2>')
break;
case h<24 :
    document.write('<h2>Good Night! </h2>');
break;
}