所有栏目 | 前端源码 2021Web前端自学笔记
你的位置:首页 > 前端代码 » 正文

js问候-根据当前时间弹出:早上好、中午好、晚上好

发布时间:2020-04-10 17:43:06

作品分类:js代码  弹出
1、在根目录(路径自己定义,我是图方便就放到了根目录)新建一个js文件(如:global.js),并复制下面代码放到到里面

//头部问候语
function hello() {
    today=new Date();
    var day; var date; var hello;
    hour=new Date().getHours()
    if(hour < 6)hello='凌晨好,'
    else if(hour < 9)hello='早上好,'
    else if(hour < 12)hello='上午好,'
    else if(hour < 14)hello='中午好,'
    else if(hour < 17)hello='下午好,'
    else if(hour < 19)hello='傍晚好,'
    else if(hour < 22)hello='晚上好,'
    else {hello='夜深了,'}
    var webUrl = webUrl;
    document.write(' '+hello);
}

2、在<head>和</head>引用js
   <script type="text/javascript" src="[!--news.url--]global.js"></script>


3、在<body>和</body>间合适的位置放入下面的代码
   <script>hello()</script>

Top