HTML学习知识点大全(终)

简介: 教程来源 https://app-acda5zfcddz5.appmiaoda.com 本文系统梳理HTML核心知识:涵盖全局属性(含ARIA)、Web Components、离线应用、结构化数据、推送通知;详解最佳实践(性能、可访问性、SEO)及HTML5/最新语义化元素,助开发者构建现代、健壮、无障碍的Web应用。

十、全局属性

10.1 常用全局属性

<!-- class - 类名(CSS/JS 选择) -->
<div class="container main-content">内容</div>

<!-- id - 唯一标识符 -->
<div id="unique-element">唯一元素</div>

<!-- style - 内联样式 -->
<div style="color: red; font-size: 16px;">红色文字</div>

<!-- title - 提示信息 -->
<abbr title="HyperText Markup Language">HTML</abbr>

<!-- lang - 语言声明 -->
<html lang="zh-CN">
<p lang="en">Hello World</p>

<!-- data-* - 自定义数据属性 -->
<div data-user-id="12345" data-role="admin">用户信息</div>
<script>
    const div = document.querySelector('div');
    console.log(div.dataset.userId); // "12345"
    console.log(div.dataset.role);    // "admin"
</script>

<!-- hidden - 隐藏元素 -->
<div hidden>隐藏内容</div>

<!-- contenteditable - 可编辑内容 -->
<div contenteditable="true">双击编辑这段文字</div>

<!-- draggable - 可拖拽 -->
<div draggable="true">拖拽我</div>

<!-- spellcheck - 拼写检查 -->
<textarea spellcheck="true">检查拼写</textarea>

<!-- tabindex - Tab 键顺序 -->
<input type="text" tabindex="1">
<input type="text" tabindex="2">
<button tabindex="3">按钮</button>

<!-- accesskey - 快捷键 -->
<a href="/" accesskey="h">首页 (Alt+H)</a>

<!-- dir - 文字方向 -->
<p dir="rtl">这段文字从右向左显示</p>

<!-- translate - 翻译控制 -->
<p translate="no">这段文字不翻译</p>

10.2 可访问性属性(ARIA)

<!-- 角色属性 -->
<nav role="navigation">导航</nav>
<main role="main">主要内容</main>

<!-- 标签属性 -->
<label for="username">用户名:</label>
<input type="text" id="username" aria-label="用户名输入框">

<!-- 描述属性 -->
<input type="text" aria-describedby="help-text">
<span id="help-text">请输入3-20个字符</span>

<!-- 状态属性 -->
<button aria-disabled="true">禁用按钮</button>
<div aria-hidden="true">屏幕阅读器忽略</div>

<!-- 实时区域 -->
<div aria-live="polite" aria-atomic="true">
    <!-- 动态内容 -->
</div>

<!-- 展开/折叠 -->
<button aria-expanded="false" aria-controls="panel">展开</button>
<div id="panel" hidden>面板内容</div>

十一、高级特性

11.1 Web Components

<!-- 自定义元素 -->
<my-element name="World"></my-element>

<script>
    class MyElement extends HTMLElement {
        constructor() {
            super();
            const shadow = this.attachShadow({ mode: 'open' });
            shadow.innerHTML = `
                <style>
                    div {
                        color: blue;
                    }
                </style>
                <div>Hello, ${this.getAttribute('name')}</div>
            `;
        }
    }
    customElements.define('my-element', MyElement);
</script>

<!-- 模板元素 -->
<template id="card-template">
    <style>
        .card {
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 16px;
        }
    </style>
    <div class="card">
        <slot name="title">默认标题</slot>
        <slot name="content">默认内容</slot>
    </div>
</template>

<div id="card-container"></div>
<script>
    const template = document.getElementById('card-template');
    const clone = template.content.cloneNode(true);
    document.getElementById('card-container').appendChild(clone);
</script>

11.2 离线应用

<!-- 缓存清单 -->
<!DOCTYPE html>
<html manifest="cache.manifest">
<head>
    <title>离线应用</title>
</head>
<body>
    <!-- 页面内容 -->
</body>
</html>

<!-- cache.manifest 文件 -->
CACHE MANIFEST
# 版本 1.0
CACHE:
index.html
style.css
script.js
logo.png

NETWORK:
*
/api/

FALLBACK:
/offline.html

11.3 微数据与结构化数据

<!-- 微数据(Schema.org) -->
<div itemscope itemtype="https://schema.org/Person">
    <span itemprop="name">张三</span>
    <span itemprop="jobTitle">软件工程师</span>
    <a href="mailto:zhang@example.com" itemprop="email">zhang@example.com</a>
</div>

<!-- 商品信息 -->
<div itemscope itemtype="https://schema.org/Product">
    <span itemprop="name">笔记本电脑</span>
    <div itemprop="offers" itemscope itemtype="https://schema.org/Offer">
        <span itemprop="price">5999</span>
        <span itemprop="priceCurrency">CNY</span>
    </div>
</div>

<!-- 文章信息 -->
<article itemscope itemtype="https://schema.org/Article">
    <h1 itemprop="headline">文章标题</h1>
    <meta itemprop="datePublished" content="2024-01-01">
    <div itemprop="author" itemscope itemtype="https://schema.org/Person">
        <span itemprop="name">作者名</span>
    </div>
    <div itemprop="articleBody">
        文章内容...
    </div>
</article>

11.4 推送通知

<!-- 请求通知权限 -->
<button onclick="requestNotification()">启用通知</button>

<script>
    function requestNotification() {
        if ('Notification' in window) {
            Notification.requestPermission().then(permission => {
                if (permission === 'granted') {
                    new Notification('通知标题', {
                        body: '通知内容',
                        icon: 'icon.png'
                    });
                }
            });
        }
    }
</script>

<!-- Service Worker -->
<script>
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('/sw.js')
            .then(registration => {
                console.log('Service Worker 注册成功');
            });
    }
</script>

十二、HTML 最佳实践

12.1 性能优化

<!-- 1. 压缩 HTML -->
<!-- 删除注释、多余空格和换行 -->

<!-- 2. 资源预加载 -->
<link rel="preload" href="critical.css" as="style">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="dns-prefetch" href="https://api.example.com">

<!-- 3. 延迟加载 -->
<img src="image.jpg" loading="lazy" alt="懒加载图片">
<iframe src="video.html" loading="lazy"></iframe>

<!-- 4. 异步加载脚本 -->
<script src="analytics.js" async></script>
<script src="app.js" defer></script>

<!-- 5. 内联关键 CSS -->
<style>
    /* 首屏关键样式 */
    .hero { background: #000; }
    .header { height: 60px; }
</style>

<!-- 6. 使用 WebP 格式图片 -->
<picture>
    <source srcset="image.webp" type="image/webp">
    <img src="image.jpg" alt="图片">
</picture>

12.2 可访问性

<!-- 1. 语义化标签 -->
<nav>导航</nav>
<main>主要内容</main>
<article>文章</article>
<aside>侧边栏</aside>

<!-- 2. 替代文本 -->
<img src="chart.png" alt="2024年销售数据图表">

<!-- 3. 标题层级 -->
<h1>页面标题</h1>
<h2>章节标题</h2>
<h3>子章节</h3>

<!-- 4. 表单标签 -->
<label for="name">姓名:</label>
<input type="text" id="name" name="name">

<!-- 5. 键盘可访问 -->
<a href="/" accesskey="h">首页 (Alt+H)</a>
<button tabindex="0">可聚焦按钮</button>

<!-- 6. ARIA 增强 -->
<button aria-label="关闭" aria-expanded="false">✕</button>
<div role="alert" aria-live="assertive">错误消息</div>

<!-- 7. 跳过导航 -->
<a href="#main" class="skip-link">跳转到主要内容</a>
<main id="main">...</main>

<!-- 8. 颜色对比度 -->
<p style="color: #333; background: #fff;">高对比度文字</p>

12.3 SEO 优化

<!-- 1. 标题和描述 -->
<title>页面标题 - 网站名称</title>
<meta name="description" content="页面描述,包含关键词">

<!-- 2. 语义化结构 -->
<article>
    <h1>文章标题</h1>
    <p>文章内容...</p>
</article>

<!-- 3. 规范链接 -->
<link rel="canonical" href="https://example.com/page.html">

<!-- 4. 移动端适配 -->
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- 5. 结构化数据 -->
<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@type": "Article",
    "headline": "文章标题",
    "author": "作者",
    "datePublished": "2024-01-01"
}
</script>

<!-- 6. 面包屑导航 -->
<nav aria-label="breadcrumb">
    <ol>
        <li><a href="/">首页</a></li>
        <li><a href="/category">分类</a></li>
        <li>当前页面</li>
    </ol>
</nav>

<!-- 7. 图片优化 -->
<img src="image.jpg" 
     alt="图片描述" 
     width="800" 
     height="600" 
     loading="lazy">

<!-- 8. robots.txt 和 sitemap.xml -->
<!-- robots.txt -->
User-agent: *
Allow: /
Sitemap: https://example.com/sitemap.xml

十三、HTML 新特性

13.1 HTML5 新元素

<!-- 结构元素 -->
<header>头部</header>
<nav>导航</nav>
<main>主要内容</main>
<article>文章</article>
<section>区块</section>
<aside>侧边栏</aside>
<footer>页脚</footer>

<!-- 多媒体元素 -->
<video>视频</video>
<audio>音频</audio>
<canvas>画布</canvas>
<svg>矢量图</svg>

<!-- 表单新类型 -->
<input type="email">
<input type="tel">
<input type="url">
<input type="number">
<input type="range">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="month">
<input type="week">
<input type="time">
<input type="search">

<!-- 语义化元素 -->
<mark>高亮</mark>
<time>时间</time>
<progress>进度</progress>
<meter>度量</meter>
<details>详情</details>
<summary>摘要</summary>
<dialog>对话框</dialog>

13.2 HTML 最新特性

<!-- 懒加载 -->
<img loading="lazy" src="image.jpg" alt="图片">
<iframe loading="lazy" src="page.html"></iframe>

<!-- 原生延迟加载 -->
<script defer src="script.js"></script>

<!-- 图片响应式 -->
<img srcset="small.jpg 480w, large.jpg 1080w" 
     sizes="(max-width: 600px) 480px, 1080px" 
     src="fallback.jpg" 
     alt="响应式图片">

<!-- 表单属性 -->
<input type="text" autocomplete="off">
<input type="text" spellcheck="true">
<input type="text" inputmode="numeric">

<!-- 跨平台特性 -->
<picture>
    <source srcset="dark-image.webp" media="(prefers-color-scheme: dark)">
    <img src="light-image.webp" alt="图片">
</picture>

<!-- 交互元素 -->
<details>
    <summary>点击展开</summary>
    <p>隐藏内容</p>
</details>

HTML 作为 Web 的基石,从诞生至今不断演进,已经发展成为一套功能强大、语义丰富、可访问性高的标记语言。本文系统性地梳理了 HTML 的核心知识点,从基础结构到高级特性,从表单处理到多媒体嵌入,帮助开发者建立完整的知识体系。
来源:
https://app-acda5zfcddz5.appmiaoda.com

相关文章
|
2月前
|
人工智能 安全 JavaScript
【含最新open claw安装包】 5 分钟快速上手Windows 一键部署 OpenClaw 教程
五分钟让您快速上手安装open claw(小龙虾),简单易上手,小白也可快速安装!
【含最新open claw安装包】    5 分钟快速上手Windows 一键部署 OpenClaw 教程
|
3月前
|
人工智能 Linux API
【OpenClaw保姆级教程】1分钟阿里云Mac/Linux/Wind11部署接入免费大模型API+Skill 集成+常见问题速答
OpenClaw(Clawdbot)作为开源的AI Agent框架,因本地部署的隐私性、功能扩展的灵活性被称作AI圈的“小龙虾”,而Skills作为其核心能力插件,更是让这款工具从简单的对话AI升级为能落地实际工作流的智能助手。2026年版本的OpenClaw进一步优化了多系统适配性与技能生态,同时可无缝对接阿里云百炼免费大模型资源,让零基础用户也能完成本地部署与功能配置。本文将从OpenClaw核心概念解析、多系统本地部署流程、阿里云百炼API配置、Skills技能插件使用、常见问题解答五个维度,为新手提供完整的零基础使用指南,全程包含可直接执行的代码命令,覆盖MacOS、Linux、Win
1610 0
|
5月前
|
人工智能 小程序 机器人
阿里云无影云电脑部署Moltbot全流程指南:从套餐购买到多消息通道验证
Moltbot(原Clawdbot)作为一款能理解自然语言、调用工具执行任务的AI Agent,在开发者群体中备受关注。阿里云无影云电脑推出的专属部署方案,通过预装镜像、简化配置步骤,让普通用户无需复杂环境搭建,3步即可启用Moltbot,还支持钉钉、QQ等常用消息通道互动,同时解决了本地部署时设备休眠、断网导致的Agent离线问题。本文结合官方教程与实操经验,用通俗语言拆解从套餐购买到功能验证的完整流程,同时说明钉钉、QQ通道的配置细节,帮助用户顺利落地这款AI助理。
1517 0
|
9天前
|
消息中间件 NoSQL 中间件
软件开发进阶技能之分布式与高并发(二)
教程来源 https://oplhc.cn/ 消息队列(MQ)是分布式系统核心中间件,以异步通信实现服务解耦、流量削峰与最终一致性。支持可靠投递、幂等消费与死信处理,广泛应用于秒杀、日志收集等高并发场景。
|
9天前
|
JSON Prometheus 监控
程序员进阶工程师必备技能之工程化与研发效率建设(五)
教程来源 https://aescc.cn/ 本节构建了完整的可观测性与效能度量体系:通过结构化JSON日志(含request_id/trace_id上下文)、Prometheus指标采集(HTTP/DB/业务维度)及OpenTelemetry分布式追踪;并集成DORA四大核心指标、开发者体验度量与可视化效能仪表板,实现从系统到团队的全栈监控与持续改进闭环。
|
1月前
|
XML 前端开发 程序员
初级程序员必备的十大技能之 API 接口与前后端联调(一)
教程来源 http://qeext.cn/ 本文系统讲解API设计规范(RESTful/GraphQL)、HTTP协议核心(方法、状态码、头信息)、前后端联调流程及调试工具,助你打造标准化、高可用接口,打破前后端协作孤岛。
|
9天前
|
缓存 NoSQL 算法
软件开发进阶技能之分布式与高并发(四)
教程来源本节详解分布式系统两大核心算法:一致性哈希(解决缓存扩缩容时数据重映射问题,通过哈希环+虚拟节点提升均衡性)与雪花算法(生成64位全局唯一、趋势递增ID)。附Java精简实现及Redis Cluster、Cassandra等实际应用对比。
|
1月前
|
Linux 程序员 网络安全
初级程序员必备的十大技能之基础 Linux 命令(一)
教程来源 https://qcycj.cn/ 本文系统讲解程序员必备的Linux核心命令,涵盖文件操作、文本处理、权限管理、进程与网络工具等,结合原理、参数详解及实战案例,助你高效部署、排查与运维——无论用Windows还是macOS,Linux都是程序员不可或缺的“第二操作系统”。
|
9天前
|
JavaScript 安全 Java
软件开发进阶技能之编程语言深度运用(一)
教程来源 http://xbivx.cn/ 本文聚焦编程进阶核心——从“会用”到“用好”的跃迁。通过深度解析类型系统(泛型、类型推断、ADT/模式匹配)、内存、并发、函数式等共性机制,结合Java/Python/TS/Go实战示例,助开发者写出更安全、高效、优雅的代码。
109 0

热门文章

最新文章