HTMLproperty

LearningHTMLproperty usingmethod and best practices

1. HTMLpropertyoverview

HTMLproperty is providing关于HTML元素 额 out information features, 它们总 is in 开始tagin指定, 可以modify元素 behavior or out 观.

1.1 property basic语法

property basic语法格式 for :

<元素名 property名="property值"> in 容</元素名>

例such as, 链接元素 hrefproperty:

<a href="https://www.example.com">访问example网站</a>

1.2 property 特点

  • property名不区分 big small 写, 但建议using small 写 (符合HTML5标准)
  • property值通常需要用引号package围, 单引号 or 双引号都可以
  • such as果property值本身package含引号, 则需要using另一种引号 or 转义
  • has 些property is booleanproperty, 不需要指定值, 只需要存 in 即可

2. 全局property

全局property is 可以application于所 has HTML元素 property, 无论元素class型such as何.

2.1 常用全局property

property describes example
id 元素 唯一标识符 <div id="header">
class 元素 class名, 用于CSS and JavaScript <div class="container main">
style in 联CSS样式 <div style="color: red;">
title 元素 提示文本 <img title="examplegraph像">
lang 元素 language <html lang="zh-CN">
dir 文本方向 <div dir="rtl">
data-* 自定义dataproperty <div data-user-id="123">
hidden 隐藏元素 <div hidden>
accesskey 元素 fast 捷键 <button accesskey="s">
tabindex 元素 Tab键顺序 <input tabindex="1">

2. 常用元素property

2.1 链接元素property

<a href="https://www.example.com" target="_blank" rel="noopener noreferrer" title="example网站">访问example网站</a>
  • href: 链接目标URL
  • target: 链接打开方式 (_blank, _self, _parent, _top)
  • rel: 链接 and 目标 relationships
  • title: 链接提示文本

2.2 graph像元素property

<img src="image.jpg" alt="examplegraph像" width="300" height="200" loading="lazy">
  • src: graph像sourcesURL
  • alt: graph像替代文本 ( important 可访问性property)
  • width: graph像宽度
  • height: graph像 high 度
  • loading: 加载方式 (lazy, eager)

2.3 表单元素property

<input type="text" name="username" id="username" placeholder="请输入user名" required minlength="3" maxlength="20">
  • type: 输入class型
  • name: 表单控件名称
  • id: 元素唯一标识符
  • placeholder: 输入提示文本
  • required: is 否必填
  • minlength: 最 small long 度
  • maxlength: 最 big long 度

实践case: usingpropertycreation一个完整 链接

creation一个package含 many 个property 链接元素, 确保它具 has 良 good 可访问性 and user体验:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>链接property实践</title>
</head>
<body>
    <h1>链接propertyexample</h1>
    
    <!-- 普通链接 -->
    <p><a href="https://www.example.com">普通链接</a></p>
    
    <!--  new 窗口打开 链接 -->
    <p><a href="https://www.example.com" target="_blank" rel="noopener noreferrer"> new 窗口打开 链接</a></p>
    
    <!-- 带 has 提示文本 链接 -->
    <p><a href="https://www.example.com" title="访问example网站">带 has 提示文本 链接</a></p>
    
    <!--  in 部锚点链接 -->
    <p><a href="#section2">跳转 to 第二部分</a></p>
    
    <div style="height: 500px; background-color: #f0f0f0;">
        <h2 id="section2">第二部分</h2>
        <p>这 is 第二部分  in 容</p>
    </div>
</body>
</html>

3. 自定义dataproperty

HTML5引入了自定义dataproperty, 允许你 in 元素 on store自定义data, 这些data可以throughJavaScript访问.

3.1 自定义dataproperty 语法

<div data-user-id="123" data-user-name="张三" data-user-age="25">userinformation</div>

3.2 throughJavaScript访问自定义dataproperty

// 获取元素
const userElement = document.querySelector('div');

// throughdataset访问自定义dataproperty
const userId = userElement.dataset.userId;      // 123
const userName = userElement.dataset.userName;  // 张三
const userAge = userElement.dataset.userAge;    // 25

// modify自定义dataproperty
userElement.dataset.userAge = '26';

// 添加 new  自定义dataproperty
userElement.dataset.userEmail = 'zhangsan@example.com';

4. property best practices

4.1 命名规范

  • using small 写字母命名property
  • property值using引号package围 (推荐using双引号)
  • 避免using big small 写混合 property名

4.2 可访问性考虑

  • for graph像添加altproperty
  • for 表单元素添加labeltag and forproperty
  • usingtitleproperty for complex 元素providing额 out 说明
  • for 链接添加titleproperty (当链接文本不明确时)

4.3 performance考虑

  • 避免过度using in 联styleproperty (优先using out 部CSS)
  • for graph像指定width and heightproperty, reducing布局偏移
  • usingloading="lazy"propertylatency加载不 in 视口in graph像

互动练习: creation一个带 has property 表单

1. creation一个HTML表单, package含以 under 字段, 并 for 每个字段添加适当 property:
  1. user名输入框 (必填, 最 small long 度3, 最 big long 度20)
  2. 电子email输入框 (必填, class型 for email)
  3. password输入框 (必填, 最 small long 度6, class型 for password)
  4. 年龄输入框 (class型 for number, 最 small 值18, 最 big 值100)
  5. 兴趣爱 good 复选框组 (至 few 选择一个)
  6. submitting按钮
2. 确保表单具 has 以 under features:
  • 所 has 输入字段都 has for 应 tag
  • 添加适当 placeholder文本
  • for 表单添加id and nameproperty
  • for 每个输入字段添加唯一 id
  • 添加必要 verificationproperty

5. HTML5 new property

5.1 表单相关 new property

  • placeholder: 输入框提示文本
  • required: 必填字段
  • minlength: 最 small 输入 long 度
  • maxlength: 最 big 输入 long 度
  • min: 最 small 值 (用于number, 日期etc.)
  • max: 最 big 值 (用于number, 日期etc.)
  • step: 步 long (用于number, 日期etc.)
  • pattern: 正则表达式verification模式
  • autocomplete: 自动completionfunctions
  • autofocus: 自动获得焦点

5.2 otherHTML5 new property

  • contenteditable: 允许user编辑元素 in 容
  • draggable: 元素 is 否可拖动
  • spellcheck: is 否启用拼写check
  • translate: is 否翻译元素 in 容
  • sandbox: iframesecurity限制
  • srcset: response式graph像sources集
  • sizes: response式graph像尺寸