html5中新增加的表单元素怎么用

发布时间:2022-04-25 13:41:21 作者:iii
来源:亿速云 阅读:250

HTML5中新增加的表单元素怎么用

HTML5 是 HTML 标准的最新版本,它引入了许多新的表单元素和属性,使得开发者能够更轻松地创建功能丰富、用户友好的表单。这些新元素不仅提高了表单的可用性,还增强了用户体验。本文将详细介绍 HTML5 中新增加的表单元素及其使用方法。

1. <input> 类型的新增

HTML5 为 <input> 元素引入了多种新的类型,这些类型不仅提供了更好的语义化,还能在支持的浏览器中自动进行输入验证。

1.1 email

email 类型用于输入电子邮件地址。浏览器会自动验证输入的内容是否符合电子邮件格式。

<label for="email">Email:</label>
<input type="email" id="email" name="email" required>

1.2 url

url 类型用于输入 URL 地址。浏览器会自动验证输入的内容是否符合 URL 格式。

<label for="url">Website:</label>
<input type="url" id="url" name="url" required>

1.3 number

number 类型用于输入数字。浏览器通常会提供一个数字输入框,用户可以通过上下箭头来调整数值。

<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" max="10">

1.4 range

range 类型用于输入一个范围内的数值。它通常显示为一个滑块。

<label for="volume">Volume:</label>
<input type="range" id="volume" name="volume" min="0" max="100">

1.5 date

date 类型用于输入日期。浏览器通常会提供一个日期选择器。

<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">

1.6 time

time 类型用于输入时间。浏览器通常会提供一个时间选择器。

<label for="appt">Appointment time:</label>
<input type="time" id="appt" name="appt">

1.7 color

color 类型用于选择颜色。浏览器通常会提供一个颜色选择器。

<label for="favcolor">Favorite color:</label>
<input type="color" id="favcolor" name="favcolor">

1.8 search

search 类型用于输入搜索关键字。它通常与搜索框的样式相匹配。

<label for="search">Search:</label>
<input type="search" id="search" name="search">

1.9 tel

tel 类型用于输入电话号码。浏览器通常会提供一个电话号码输入框。

<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone">

1.10 week

week 类型用于输入一年中的某一周。浏览器通常会提供一个周选择器。

<label for="week">Select a week:</label>
<input type="week" id="week" name="week">

1.11 month

month 类型用于输入月份。浏览器通常会提供一个月份选择器。

<label for="month">Select a month:</label>
<input type="month" id="month" name="month">

2. 新的表单元素

除了 <input> 类型的新增,HTML5 还引入了一些新的表单元素,这些元素提供了更丰富的功能和更好的语义化。

2.1 <datalist>

<datalist> 元素用于为 <input> 元素提供一个预定义的选项列表。用户可以从列表中选择,也可以手动输入。

<label for="browser">Choose your browser:</label>
<input list="browsers" id="browser" name="browser">
<datalist id="browsers">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Safari">
  <option value="Edge">
  <option value="Opera">
</datalist>

2.2 <output>

<output> 元素用于显示计算结果或其他输出内容。它通常与 JavaScript 结合使用。

<form oninput="result.value=parseInt(a.value)+parseInt(b.value)">
  <input type="number" id="a" name="a" value="0">
  + <input type="number" id="b" name="b" value="0">
  = <output name="result" for="a b">0</output>
</form>

2.3 <progress>

<progress> 元素用于显示任务的进度。它通常用于表示文件上传或下载的进度。

<label for="file">File progress:</label>
<progress id="file" max="100" value="70"> 70% </progress>

2.4 <meter>

<meter> 元素用于显示一个范围内的标量值。它通常用于表示磁盘使用情况、投票结果等。

<label for="disk_c">Disk usage C:</label>
<meter id="disk_c" value="2" min="0" max="10">2 out of 10</meter><br>
<label for="disk_d">Disk usage D:</label>
<meter id="disk_d" value="0.6">60%</meter>

3. 新的表单属性

HTML5 还为表单元素引入了一些新的属性,这些属性增强了表单的功能和用户体验。

3.1 placeholder

placeholder 属性用于在输入框中显示提示文本,当用户开始输入时,提示文本会自动消失。

<label for="username">Username:</label>
<input type="text" id="username" name="username" placeholder="Enter your username">

3.2 required

required 属性用于指定输入框为必填项。如果用户未填写该字段,表单将无法提交。

<label for="username">Username:</label>
<input type="text" id="username" name="username" required>

3.3 autofocus

autofocus 属性用于指定页面加载时自动聚焦到该输入框。

<label for="username">Username:</label>
<input type="text" id="username" name="username" autofocus>

3.4 autocomplete

autocomplete 属性用于控制输入框的自动填充功能。可以设置为 onoff

<label for="email">Email:</label>
<input type="email" id="email" name="email" autocomplete="on">

3.5 pattern

pattern 属性用于指定输入框的正则表达式验证规则。用户输入的内容必须符合该正则表达式。

<label for="password">Password:</label>
<input type="password" id="password" name="password" pattern="[A-Za-z0-9]{8,}" title="Password must be at least 8 characters long and contain only letters and numbers.">

3.6 minmax

minmax 属性用于指定输入框的最小值和最大值。适用于 numberrangedate 等类型的输入框。

<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" max="10">

3.7 step

step 属性用于指定输入框的步长。适用于 numberrangedate 等类型的输入框。

<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" max="10" step="2">

4. 总结

HTML5 引入了许多新的表单元素和属性,这些新特性不仅提高了表单的可用性,还增强了用户体验。通过使用这些新元素和属性,开发者可以更轻松地创建功能丰富、用户友好的表单。在实际开发中,建议根据具体需求选择合适的表单元素和属性,以确保表单的功能和用户体验达到最佳效果。

推荐阅读:
  1. HTML5中新增加的标签有哪些
  2. HTML5新表单元素怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

html5

上一篇:PHP的session反序列化漏洞分析

下一篇:Windows server 2012 R2双AD域搭建的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》