js问候-早上好、中午好、晚上好

黄文博客 / 2024-10-05 / 原文

要在帝国CMS中实现头部问候语功能,可以通过以下几个步骤完成:

  1. 创建 global.js 文件
  2. 在页面中引入 global.js 文件
  3. 调用 hello 函数

步骤详解

1. 创建 global.js 文件

  1. 新建 global.js 文件

    • 在网站根目录下新建一个名为 global.js 的文件。
    • 将以下代码复制到 global.js 文件中:
    // 头部问候语
    function hello() {
        var today = new Date();
        var hour = today.getHours();
        var hello;
    
        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 = '夜深了,';
        }
    
        document.write(' ' + hello);
    }

2. 在页面中引入 global.js 文件

  1. 在 <head> 标签中引入 global.js 文件

    • 在页面的 <head> 标签中添加以下代码:
    html
     
    <script type="text/javascript" src="[!--news.url--]global.js"></script>

    其中 [!--news.url--] 是帝国CMS的变量,用于获取当前网站的 URL。