第 10章表单元素[上]

摘要:
1.表单元素总汇
2.表单元素解析
本章主要探讨 HTML5中表单元素,表单元素用于获取用户的输入数据。

一.表单元素总汇
在 HTML5的表单中,提供了各种可供用户输入的表单控件。
第 10章表单元素[上]

二.表单元素解析
1.<form>定义表单,本身具有提交功能
<form method=”post” action=”http://www.haosou.com/”>

<button>提交</button>

</form>

解释:<form>元素主要是定义本身是一组表单。

第 10章表单元素[上]

//使用 get提交数据
method=”get”
//丧失自动提示功能
autocomplete=”off”
//使用_blank新建目标
target=”_blank”
2.<input>表示用户输入数据
<input name=”user”>

解释:<input>元素默认情况会出现一个单行文本框,有五个属性。
第 10章表单元素[上]

//聚焦光标
<input name=”user” autofocus>
//禁用 input
<input name=”user” disabled>

//禁止自动完成
<input name=”user” autocomplete=”off”>
//表单外的 input
<form method=”get” id=”register”>

</form>
<input name=”email” form=”register”>
3.<label>添加说明标签

<p><label for=”user”>用户名:<input id=”user” name=”user”></label></p>

<p><label>用户名:<input id=”user” name=”user”></label></p>
<p><label for=”user”></label> 用户名:<input id=”user” name=”user”></p>
解释:<label>元素可以关联 input,让用户体验更好。且更加容易设置 CSS样式。
4.<fieldset>对表单进行编组
<fieldset>…</fieldset>

解释:<fieldset>元素可以将一些表单元素组织在一起,形成一个整体。

第 10章表单元素[上]

5.<legend>添加分组说明标签
<fieldset>
<legend>注册表单</legend>
</fieldset>
解释:<legend>元素给分组加上一个标题。
6.<button>添加按钮
<button type=”submit”></button>

解释:<button>元素添加一个按钮,type属性有如下几个值:

第 10章表单元素[上]

//提交表单
<button type=”submit”>提交</button>
//重置表单

<button type=”reset”>重置</button>
//普通按钮
<button type=”button”>按钮</button>

对于 type属性为 submit时,按钮还会提供额外的属性。

第 10章表单元素[上]

//表单外关联提交

<button type=”submit” form=”register”>提交</button>