学习要点:
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>

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
