第 10 章 表单元素[上] - 水之原
学习要点:
1.表单元素总汇
2.表单元素解析
主讲教师:李炎恢
本章主要探讨 HTML5 中表单元素,表单元素用于获取用户的输入数据。
一.表单元素总汇
在 HTML5 的表单中,提供了各种可供用户输入的表单控件。
元素名称 |
说明 |
form |
表示 HTML 表单 |
input |
表示用来收集用户输入数据的控件 |
textarea |
表示可以输入多行文本的控件 |
select |
表示用来提供一组固定的选项 |
option |
表示提供提供一个选项 |
optgroup |
表示一组相关的 option 元素 |
button |
表示可用来提交或重置的表单按钮(或一般按钮) |
datalist |
定义一组提供给用户的建议值 |
fieldset |
表示一组表单元素 |
legend |
表示 fieldset 元素的说明性标签 |
label |
表示表单元素的说明标签 |
output |
表示计算结果 |
二.表单元素解析
1.定义表单
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> action</span><span style="color: #0000ff;">="http://www.haosou.com/"</span><span style="color: #0000ff;">></span> <br><span style="color: #0000ff;"> <span style="color: #800000;">button</span><span style="color: #0000ff;">></span>提交<span style="color: #0000ff;"></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span></span></span>
解释:
元素名称 |
说明 |
action |
表示表单提交的页面 |
method |
表示表单的请求方式:有 POST 和 GET 两种,默认 GET |
enctype |
表示浏览器对发送给服务器的数据所采用的编码格式。有三种:application/x-www-form-urlencoded(默认编码,不能将文件上传到服务器)、multipart/form-data(用于上传文件到服务器)、text/plain(未规范的编码,不建议使用,不同浏览器理解不同) |
name |
设置表单名称,以便程序调用 |
target |
设置提交时的目标位置:_blank、_parent、_self、_top |
autocomplete |
设置浏览器记住用户输入的数据,实现自动完成表单。默认为 on 自动完成,如果不想自动完成则设置 off。 |
novalidate |
设置是否执行客户端数据有效性检查,后面课程讲解。 |
//使用 get 提交数据
method="get"
//丧失自动提示功能
autocomplete="off"
//使用_blank 新建目标
target="_blank"
2.表示用户输入数据
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="user"</span><span style="color: #0000ff;">></span></span>
解释:元素默认情况会出现一个单行文本框,有五个属性。
属性名称 |
说明 |
autofocus |
让光标聚焦在某个 input 元素上,让用户直接输入 |
disabled |
禁用 input 元素 |
autocomplete |
单独设置 input 元素的自动完成功能 |
form |
让表单外的元素和指定的表单挂钩提交 |
type |
input 元素的类型,内容较多,将在下节课展开讲解 |
name |
定义 input 元素的名称,以便接收到相应的值 |
//聚焦光标
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="user"</span><span style="color: #ff0000;"> autofocus</span><span style="color: #0000ff;">></span></span>
//禁用 input
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="user"</span><span style="color: #ff0000;"> disabled</span><span style="color: #0000ff;">></span></span>
//禁止自动完成
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="user"</span><span style="color: #ff0000;"> autocomplete</span><span style="color: #0000ff;">="off"</span><span style="color: #0000ff;">></span></span>
//表单外的 input
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">method</span><span style="color: #0000ff;">="get"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="register"</span><span style="color: #0000ff;">></span><span style="color: #000000;"> ... </span><span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="email"</span><span style="color: #ff0000;"> form</span><span style="color: #0000ff;">="register"</span><span style="color: #0000ff;">></span></span></span>
3.添加说明标签
<span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="user"</span><span style="color: #0000ff;">></span>用户名:<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="user"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="user"</span><span style="color: #0000ff;">></span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span></span></span></span>
解释:
4.对表单进行编组
<span style="color: #0000ff;"><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">></span>...<span style="color: #0000ff;"></span><span style="color: #800000;">fieldset</span><span style="color: #0000ff;">></span></span>
解释:
//普通按钮
<span style="color: #0000ff;"><span style="color: #800000;">button </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="button"</span><span style="color: #0000ff;">></span>按钮<span style="color: #0000ff;"></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span> </span>
对于 type 属性为 submit 时,按钮还会提供额外的属性。
属性名称 |
说明 |
form |
指定按钮关联的表单 |
formaction |
覆盖 form 元素的 action 属性 |
formenctype |
覆盖 form 元素的 enctype 属性 |
formmethod |
覆盖 form 元素的 method 属性 |
formtarget |
覆盖 form 元素的 target 属性 |
formnovalidate |
覆盖 form 元素的 novalidate 属性 |
//表单外关联提交
<span style="color: #0000ff;"><span style="color: #800000;">button </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> form</span><span style="color: #0000ff;">="register"</span><span style="color: #0000ff;">></span>提交<span style="color: #0000ff;"></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span></span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.
