郭雁 發表於 2026-3-10 10:23:00

Web前端开发技术第二周周二课堂笔记

<p>Web前端开发技术第二周周二课堂笔记<br>
1.新建项目<br>
文件—&gt;新建—&gt;Web项目,弹出对话框后修改名称及地址,点击完成<br>
2.新建HTML文件<br>
右击刚刚所建的Web项目—&gt;新建—&gt;HTML文件,弹出对话框后修改名称为showTime.html,单击完成<br>
3.输入代码(输出一个静态网页,能实时显示系统时间,格式包含:年月日时分秒,用Javascript实现时间实时)</p>
<pre><code class="language-HTML">&lt;!DOCTYPE html&gt;
&lt;html lang="zh-CN"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;实时系统时间显示&lt;/title&gt;
    &lt;style&gt;
      * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
      }

      body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
      }

      .time-container {
            background-color: #ffffff;
            padding: 40px 60px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            text-align: center;
      }

      .time-title {
            font-size: 24px;
            color: #333333;
            margin-bottom: 20px;
      }

      .current-time {
            font-size: 48px;
            color: #165DFF;
            font-weight: bold;
            letter-spacing: 2px;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class="time-container"&gt;
      &lt;h2 class="time-title"&gt;当前系统时间&lt;/h2&gt;
      &lt;div id="timeDisplay" class="current-time"&gt;&lt;/div&gt;
    &lt;/div&gt;

    &lt;script&gt;
      // 获取显示时间的DOM元素
      const timeDisplay = document.getElementById('timeDisplay');

      // 格式化时间函数:补零操作(确保个位数显示为两位数,如 9 → 09)
      function formatNumber(num) {
            return num &lt; 10 ? `0${num}` : num;
      }

      // 更新时间的核心函数
      function updateTime() {
            // 创建当前时间对象
            const now = new Date();
            
            // 提取年月日时分秒
            const year = now.getFullYear(); // 年(4位数)
            const month = formatNumber(now.getMonth() + 1); // 月(0-11,需+1)
            const day = formatNumber(now.getDate()); // 日
            const hour = formatNumber(now.getHours()); // 时
            const minute = formatNumber(now.getMinutes()); // 分
            const second = formatNumber(now.getSeconds()); // 秒

            // 拼接成指定格式的时间字符串
            const timeStr = `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
            
            // 将时间显示到页面上
            timeDisplay.textContent = timeStr;
      }

      // 1. 页面加载时立即执行一次,避免初始空白
      updateTime();
      // 2. 设置定时器,每1000毫秒(1秒)更新一次时间,实现实时效果
      setInterval(updateTime, 1000);
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

</code></pre>
<p>4.创建css文件<br>
右击css—&gt;新建—&gt;css文件,弹出对话框后命名为style.css,单击完成<br>
将showTime中的</p>
<pre><code class="language-HTML">* {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
      }

      body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
      }

      .time-container {
            background-color: #ffffff;
            padding: 40px 60px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            text-align: center;
      }

      .time-title {
            font-size: 24px;
            color: #333333;
            margin-bottom: 20px;
      }

      .current-time {
            font-size: 48px;
            color: #165DFF;
            font-weight: bold;
            letter-spacing: 2px;
      }

</code></pre>
<p>复制并删除,将其复制到刚刚所建的css文件中。</p>
<p>5.在showTime中刚刚删除的地方输入l单击两次回车<br>
输入内容为</p>
<pre><code class="language-HTML">&lt;link rel="stylesheet" type="text/css" href="css/style.css"/&gt;
</code></pre>
<p>6.将showTime与css文件都保存,保存后在showTime中Ctrl+R运行<br>
7.新建JS文件<br>
右击js—&gt;新建—&gt;javaScirpt文件,弹出对话框后命名,单击完成<br>
8.将showTime中</p>
<pre><code class="language-HTML">   // 获取显示时间的DOM元素
      const timeDisplay = document.getElementById('timeDisplay');

      // 格式化时间函数:补零操作(确保个位数显示为两位数,如 9 → 09)
      function formatNumber(num) {
            return num &lt; 10 ? `0${num}` : num;
      }

      // 更新时间的核心函数
      function updateTime() {
            // 创建当前时间对象
            const now = new Date();
            
            // 提取年月日时分秒
            const year = now.getFullYear(); // 年(4位数)
            const month = formatNumber(now.getMonth() + 1); // 月(0-11,需+1)
            const day = formatNumber(now.getDate()); // 日
            const hour = formatNumber(now.getHours()); // 时
            const minute = formatNumber(now.getMinutes()); // 分
            const second = formatNumber(now.getSeconds()); // 秒

            // 拼接成指定格式的时间字符串
            const timeStr = `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
            
            // 将时间显示到页面上
            timeDisplay.textContent = timeStr;
      }

      // 1. 页面加载时立即执行一次,避免初始空白
      updateTime();
      // 2. 设置定时器,每1000毫秒(1秒)更新一次时间,实现实时效果
      setInterval(updateTime, 1000);

</code></pre>
<p>复制并删除,将其复制到刚刚所建的js文件中。<br>
9.在showTime中刚刚删除的地方输入s,选中第三个回车再回车<br>
输入内容为</p>
<pre><code class="language-HTML">&lt;script src="js/js.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
</code></pre>
<p>10.将showTime与js文件都保存,保存后回到showTime中Ctrl+R运行</p>
<p>11.将时间字体改为红色</p>
<p>打开style.css,将最后的color改为red。</p>
<pre><code class="language-HTML"> .current-time {
            font-size: 48px;
            color: red;
            font-weight: bold;
            letter-spacing: 2px;
      }
</code></pre>
<p>12.将showTime与css文件都保存,保存后回到showTime中Ctrl+R运行</p><br><br>
来源:https://www.cnblogs.com/0921k/p/19695485
頁: [1]
查看完整版本: Web前端开发技术第二周周二课堂笔记