Home Web Front-end HTML Tutorial 第 10 章 表单元素[中]_html/css_WEB-ITnose

第 10 章 表单元素[中]_html/css_WEB-ITnose

Jun 24, 2016 am 11:19 AM

学习要点: 

1.type 属性总汇

 2.type 属性解析

 

主讲教师:李炎恢

 

本章主要探讨 HTML5 中表单中 input 元素的 type 属性,根据不同的值来显示不同的输入框。

 

一.type 属性总汇

input 元素可以用来生成一个供用户输入数据的简单文本框。在默认的情况下,什么样的数据均可以输入。而通过不同的属性值,可以限制输入的内容。

属性名称

说明

text

一个单行文本框,默认行为

password

隐藏字符的密码框

search

搜索框,在某些浏览器键入内容会出现叉标记取消

submit、reset、button

生成一个提交按钮、重置按钮、普通按钮

number、range

只能输入数值的框;只能输入在一个数值范围的框

checkbox、radio

复选框,用户勾选框;单选框,只能在几个中选一个

image、color

生成一个图片按钮,颜色代码按钮

email、tel、url

生成一个检测电子邮件、号码、网址的文本框

date、month、time、week、datetime、datetime-local

获取日期和时间

hidden

生成一个隐藏控件

file

生成一个上传控件

 

 二.input 元素解析

1.type  text 值时

<input type="text">
Copy after login

解释:当 type 值为 text 时,呈现的就是一个可以输入任意字符的文本框,这也是默认行为。并且,还提供了一些额外的属性。

属性名称

说明

list

指定为文本框提供建议值的 datalist 元素,其值为datalist 元素的 id 值

maxlength

设置文本框最大字符长度

pattern

用于输入验证的正则表达式

placeholder

输入字符的提示

readonly

文本框处于只读状态

disabled

文本框处于禁用状态

size

设置文本框宽度

value

设置文本框初始值

required

表明用户必须输入一个值,否则无法通过输入验证

//设置文本框长度

<input type="text" size="50">
Copy after login

//设置文本框输入字符长度

<input type="text" maxlength="10">
Copy after login

//设置文本框的初始值

<input type="text" value="初始值">
Copy after login

//设置文本框输入提示

<input type="text" placeholder="请输入内容">
Copy after login

//设置文本提供的建议值

<input list="footlist"><datalist id="footlist">    <option value="苹果">苹果</option>    <option value="桔子">桔子</option>    <option value="香蕉" label="香蕉">    <option value="梨子"></datalist>
Copy after login

//设置文本框内容为只读,可以提交数据

<input type="text" readonly>
Copy after login

//设置文本框内容不可用,不可以提交数据

<input type="text" disabled>
Copy after login

2.type password 值时

<input type="password">
Copy after login

解释:当 type 值为 password 时,一般用于密码框的输入,所有的字符都会显示星号。密码框也有一些额外属性。

属性名称

说明

maxlength

设置密码框最大字符长度

pattern

用于输入验证的正则表达式

placeholder

输入密码的提示

readonly

密码框处于只读状态

disabled

文本框处于禁用状态

size

设置密码框宽度

value

设置密码框初始值

required

表明用户必须输入同一个值

这里除了正则和验证需要放在下一节,其余和文本框一致。

3.type search

<input type="search">
Copy after login

解释:和文本框一致,在除 Firefox 浏览器的其他现代浏览器,会显示一个叉来取消搜索内容。额外属性也和 text 一致。

4.type numberrange

<input type="number"><input type="range">
Copy after login

解释:只限输入数字的文本框,不同浏览器可能显示方式不同。生成一个数值范围文本框,只是样式是拖动式。额外属性如下:

属性名称

说明

list

指定为文本框提供建议值的 datalist 元素,其值为datalist 元素的 id 值

min

设置可接受的最小值

max

设定可接受的最大值

readonly

设置文本框内容只读

required

表明用户必须输入一个值,否则无法通过输入验证

step

指定上下调节值的步长

value

指定初始值

//范围和步长

<input type="number" step="2" min="10" max="100">
Copy after login

5.type date 系列时

<input type="date"><input type="month"><input type="time"><input type="week"><input type="datetime"><input type="datetime-local">
Copy after login

解释:实现文本框可以获取日期和时间的值,但支持的浏览器不完整。我们测试 Chrome 和 Opera 支持,其他浏览器尚未支持。所以,在获取日期和时间,目前还是推荐使用 jQuery 等前端库来实现日历功能。额外属性和 number 一致。

6.type color

<input type="color">
Copy after login

解释:实现文本框获取颜色的功能,最新的现代浏览器测试后 IE 不支持,其余的都能显示一个颜色对话框提供选择。

7.type checkboxradio

音乐<input type="checkbox">体育<input type="checkbox"><input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女
Copy after login

解释:生成一个获取布尔值的复选框或固定选项的单选框。额外属性如下:

属性名称

说明

checked

设置复选框、单选框是否为勾选状态

required

表示用户必须勾选,否则无法通过验证

value

设置复选框、单选框勾选状态时提交的数据。默认为 on

//默认勾选,默认值为 1

<input type="checkbox" name="music" checked value="1">
Copy after login

8.type submitreset button

<input type="submit">
Copy after login

解释:生成一个按钮,三种模式:提交、重置和一般按钮,和

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles