3月9日

一.显示系统时间显示

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时系统时间显示</title>
    <style>
        /* 页面整体样式 */
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f2f5;
        }

        /* 时间显示容器 */
        .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 {
            color: #333333;
            margin-bottom: 20px;
            font-size: 24px;
            font-weight: 600;
        }

        /* 时间显示区域 */
        .current-time {
            font-size: 48px;
            font-weight: bold;
            color: #2c3e50;
            letter-spacing: 2px;
        }

        /* 秒数的特殊样式(可选) */
        .seconds {
            color: #e74c3c;
        }
    </style>
</head>
<body>
    <div class="time-container">
        <h2 class="time-title">当前系统时间</h2>
        <div id="timeDisplay" class="current-time"></div>
    </div>

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

        // 格式化数字:确保单个数字前面补0(如 9 → 09)
        function formatNumber(num) {
            return num.toString().padStart(2, '0');
        }

        // 更新时间的核心函数
        function updateTime() {
            // 获取当前系统时间
            const now = new Date();
            
            // 提取年月日时分秒,并格式化
            const year = now.getFullYear(); // 年(4位)
            const month = formatNumber(now.getMonth() + 1); // 月(注意月份从0开始,需+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}:<span class="seconds">${second}</span>`;
            timeDisplay.innerHTML = timeStr;
        }

        // 初始化:立即执行一次更新
        updateTime();

        // 设置定时器:每秒更新一次时间(1000毫秒 = 1秒)
        setInterval(updateTime, 1000);
    </script>
</body>
</html>

二.快捷键

1.预览ctrl+R

三.三者的作用

1.css:层叠样式表

cascading Style sheet

美化页面的作用

如何把样式联系到主文件里

<link rel="stylesheet" type="text/css" href="css/style01.css"/> 

2.img:HTML图片标签

<script src="js/js.js" type="text/javascript" charset="utf-8"></script>

3.html主文件:框架结构的作用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
	</body>
</html>
posted @ 2026-03-15 16:24  +6  阅读(8)  评论(0)    收藏  举报