初学前端要搞清楚html, CSS, javascript在页面构建中各自的作用

1.html主文件作用:构建框架结构

点击查看代码
<!DOCTYPE html>
<html>
    <head>
        <meta charset='UTF-8'>
        <title></title>
    </head> 
    <body>
    </body>
</html>

2.CSS(层叠样式表)(Cascading Style sheet )作用:
美化页面
把样式联系到主文件里的方法

点击查看代码
<link rel="stylesheet type=text/css" href="css/style01.css"/>
其中前两个“=”可省略最后一个不可省。

3.javascript作用:页面交互
把该脚本联系主文件里的方法

点击查看代码
<script src="js/js.js" type="test/javascript" charset="utf-8"></script>

4.样例代码:用js实时显示系统时间

点击查看代码
<!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>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            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 {
            font-size: 24px;
            color: #333333;
            margin-bottom: 20px;
            font-weight: 600;
        }

        .current-time {
            font-size: 48px;
            color: #1677ff;
            font-weight: 700;
            letter-spacing: 2px;
        }
    </style>
</head>
<body>
    <div class="time-container">
        <div class="time-title">当前系统时间</div>
        <div id="timeDisplay" class="current-time"></div>
    </div>

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

        // 格式化数字,确保两位数(如 9 → 09)
        function formatNumber(num) {
            return num.toString().padStart(2, '0');
        }

        // 更新时间的核心函数
        function updateTime() {
            // 获取当前系统时间
            const now = new Date();
            
            // 提取年月日时分秒并格式化
            const year = now.getFullYear();
            const month = formatNumber(now.getMonth() + 1); // 月份从0开始,需+1
            const day = formatNumber(now.getDate());
            const hours = formatNumber(now.getHours());
            const minutes = formatNumber(now.getMinutes());
            const seconds = formatNumber(now.getSeconds());

            // 拼接成指定格式的时间字符串
            const timeString = `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
            
            // 更新页面显示
            timeDisplay.textContent = timeString;
        }

        // 初始化时立即执行一次,避免页面加载后有延迟
        updateTime();

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

注意事项:
1.将样式/脚本联系到主文件里时
css和js文档中分别没有script标签,style标签

2.快捷键:先ctrl+s保存 后ctr+r运行

posted @ 2026-03-10 16:16  createcoding  阅读(4)  评论(0)    收藏  举报