一.了解表格与表单
1.表格
表格元素
- 表格是最重要的数据格式化展示重要工具, 使用频率非常高
- 列表与表格相比, 更像是一个单行单列的表格项
- 表格的数据,存储在由行与列组成的一系列单元格
- 与列表类似, 表格也是由一系列标签来描述
- 表格标签分为二类: 数据标签, 结构标签
- 数据标签:
序号 |
标签 |
描述 |
1 |
<table> |
定义表格, 必选 |
2 |
<tr> |
定义表格中的行, 必选 |
3 |
<th> |
定义表格头部中的单元格, 必选 |
4 |
<td> |
定义表格主体中的单元格, 必选 |
序号 |
标签 |
描述 |
1 |
<option> |
定义表格标题, 可选 |
2 |
<thead> |
定义表格头格, 只需定义一次, 可选 |
3 |
<tbody> |
定义表格主体, 允许定义多次, 可选 |
4 |
<tfooter> |
定义表格底, 只需定义一次, 可选 |
序号 |
属性 |
所属标签 |
描述 |
1 |
border |
<table> |
添加表格框 |
2 |
cellpadding |
<table> |
设置单元格内边距 |
2 |
cellspacing |
<table> |
设置单元格边框间隙 |
2 |
align |
不限 |
设置单元格内容水平居中 |
2 |
bgcolor |
不限 |
设置背景色 |
2.表单
1. 表单元素类型
序号 |
元素 |
名称 |
描述 |
1 |
<form> |
表单容器 |
表单应该放在该标签内提交 |
2 |
<input> |
输入控件 |
由type 属性指定控件类型 |
3 |
<label> |
控件标签 |
用于控件功能描述与内容关联 |
4 |
<select> |
下拉列表 |
用于选择预置的输入内容 |
5 |
<datalist> |
预置列表 |
用于展示预置的输入内容 |
6 |
<option> |
预置选项 |
与<select> ,<datalist> 配合 |
7 |
<textarea> |
文本域 |
多行文本框,常与富文本编辑器配合 |
8 |
<button> |
按钮 |
用于提交表单 |
2. 表单元素属性
序号 |
元素 |
属性 |
1 |
<form> |
action , method |
2 |
<label> |
for |
3 |
<input> |
type , name ,value ,placeholder … |
4 |
<select> |
name |
5 |
<datalist> |
id ,与<input list=""> 关联 |
6 |
<option> |
value , label ,selected |
7 |
<textarea> |
cols , rows ,name … |
8 |
<button> |
type ,name |
表单元素的公共属性(并非所有元素都具备)
序号 |
属性 |
描述 |
1 |
name |
元素/控件名称,用做服务器端脚本的变量名称 |
2 |
value |
提交到服务器端的数据 |
3 |
placeholder |
输入框的提示信息 |
4 |
form |
所属的表单,与<form name=""> 对应 |
5 |
autofocus |
页面加载时,自动获取焦点 |
6 |
required |
必填 / 必选项 |
7 |
readonly |
该控件内容只读 |
8 |
disabled |
是否禁用 |
3.1 常用的type
类型
序号 |
属性 |
名称 |
描述 |
1 |
type="text" |
文本框 |
默认值 |
2 |
type="password" |
密码框 |
输入内容显示为* ,不显示明文 |
3 |
type="radio" |
单选按钮 |
多个选项中仅允许提交一个(应提供默认选项) |
4 |
type="checkbox" |
复选框 |
允许同时提交一个或多个选项(应提供默认选项) |
5 |
type="hidden" |
隐藏域 |
页面不显示,但数据仍会被提交 |
6 |
type="file" |
文件上传 |
本地文件上传,有accept ,multiple 属性 |
7 |
type="submit" |
提交按钮 |
点击后会提交按钮所在的表单 |
8 |
type="reset" |
重置按钮 |
点击后会重置输入控件中的内容为默认值 |
9 |
type="button" |
自定义按钮 |
使用 JS 脚本定义按钮点击后的行为 |
3.2 其它type
类型(部分)
序号 |
属性 |
名称 |
描述 |
1 |
type="email" |
邮箱 |
输入必须是邮箱格式 |
2 |
type="number" |
整数 |
输入必须是整数,可设置范围min ,max |
3 |
type="url" |
URL 地址 |
输入内容必须是有效的 URL 格式文本 |
4 |
type="search" |
搜索框 |
通常与autocomplete 配合 |
5 |
type="hidden" |
隐藏域 |
页面不显示,但数据仍会被提交 |
6 |
type="date" |
日期控件 |
不同浏览器显示略有不同,但功能一致 |
7 |
type="color" |
调色板 |
可直接选择颜色, 不同平台略有不同 |
8 |
type="tel" |
电话号码 |
手机端会弹出数字小键盘 |
4. 注意事项
- 添加
disabled
禁用属性的字段数据不会被提交,但是readonly
只读属性的字段允许提交 - 隐藏域的内容不会在 HTML 页面中显示,但是
value
属性的数据会被提交
二.表格实现购物车
实例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>表格实战:购物车</title>
</head>
<style>
html {
font-size: 14px;
}
/* 将单元格之间的间隙去掉 */
table {
border-collapse: collapse;
width: 70%;
margin: auto;
color: #666;
font-weight: lighter;
text-align: center;
}
/* 表格线直接添加到单元格上面 */
th,
td {
border-bottom: 1px solid #ccc;
padding: 10px;
}
/* 标题样式 */
table caption {
font-size: 1.5rem;
margin-bottom: 15px;
font-weight: bolder;
color: green;
}
table th {
font-weight: lighter;
color: green;
}
table thead tr:first-of-type {
background-color: lightcyan;
}
/* 隔行变色 */
table tbody tr:nth-of-type(even) {
background-color: yellow;
}
/* 鼠标悬停效果 */
table tbody tr:hover {
background-color: pink;
}
table tfoot td {
border-bottom: none;
color: coral;
font-size: 1.2rem;
font-weight: bolder;
}
/* 结束按钮 */
body div:first-of-type {
width: 70%;
margin: auto;
}
body div:first-of-type button {
/* 靠右 */
float: right;
width: 120px;
height: 32px;
background-color: seagreen;
color: white;
border: none;
/* 设置鼠标样式 */
cursor: pointer;
}
body div:first-of-type button:hover {
background-color: coral;
font-size: 1.1rem;
}
</style>
<body>
<!-- 表格 -->
<table>
<!-- 标题 -->
<caption>
购物车
</caption>
<!-- 头部 -->
<thead>
<tr>
<th>ID</th>
<th>品名</th>
<th>单价/元</th>
<th>单位</th>
<th>数量</th>
<th>金额</th>
</tr>
</thead>
<!-- 主体 -->
<tbody>
<tr>
<td>SN-1010</td>
<td>MacBookpro 电脑</td>
<td>18999</td>
<td>台</td>
<td>1</td>
<td>18999</td>
</tr>
<tr>
<td>SN-1020</td>
<td>iPhone手机</td>
<td>4999</td>
<td>部</td>
<td>2</td>
<td>9998</td>
</tr>
<tr>
<td>SN-1030</td>
<td>智能AI音箱</td>
<td>399</td>
<td>只</td>
<td>5</td>
<td>1995</td>
</tr>
<tr>
<td>SN-1040</td>
<td>SSD移动硬盘</td>
<td>888</td>
<td>个</td>
<td>2</td>
<td>1776</td>
</tr>
<tr>
<td>SN-1050</td>
<td>黄山毛峰</td>
<td>999</td>
<td>斤</td>
<td>3</td>
<td>2997</td>
</tr>
</tbody>
<!-- 底部 -->
<tfoot>
<tr>
<td colspan="4">总计:</td>
<td>13</td>
<td>35765</td>
</tr>
</tfoot>
</table>
<!-- 结算按钮 -->
<div><button>结算</button></div>
</body>
</html>
效果预览:
三.表单实现用户注册页面
实例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>基本表单元素</title>
<style>
body {
color: #555;
}
h3 {
text-align: center;
}
form {
width: 450px;
margin: 30px auto;
border-top: 1px solid #aaa;
}
form fieldset {
border: 1px solid seagreen;
background-color: lightcyan;
box-shadow: 2px 2px 4px #bbb;
border-radius: 10px;
margin: 20px;
}
form fieldset legend {
background-color: rgb(178, 231, 201);
border-radius: 10px;
color: seagreen;
padding: 5px 15px;
}
form div {
margin: 5px;
}
form p {
text-align: center;
}
form .btn {
width: 80px;
height: 30px;
border: none;
background-color: seagreen;
color: #ddd;
}
form .btn:hover {
background-color: coral;
color: white;
cursor: pointer;
}
input:focus {
background-color: rgb(226, 226, 175);
}
</style>
</head>
<body>
<h3>用户注册</h3>
<form action="" method="POST">
<!-- 1.控件组 -->
<fieldset>
<legend>基本信息(必填)</legend>
<div>
<label for="my-username">账号:</label>
<input
type="text"
name="username"
id="my-username"
placeholder="6到15位字符"
autofocus
required
/>
</div>
<div>
<label for="email-id">邮箱:</label>
<input
type="email"
name="email"
id="email-id"
placeholder="demo@qq.com"
required
/>
</div>
<!-- 密码 -->
<div>
<label for="pwd-1">密码:</label>
<input
type="password"
name="password1"
id="pwd-1"
placeholder="不少于6位且字母+数字"
required
/>
<button onclick="ShowPwd()" id="btn" type="button">显示密码</button>
</div>
<!-- 确认密码 -->
<div>
<label for="pwd-2">确认:</label>
<input type="password" name="password2" id="pwd-2" required />
</div>
</fieldset>
<fieldset>
<legend>扩展信息(选填)</legend>
<div>
<label
>生日:
<input type="date" name="birthday" />
</label>
</div>
<div>
<!-- 单选按钮 -->
<label for="">性别:</label>
<!-- 单选按钮中的name属性必须相同 -->
<input type="radio" name="gender" value="male" id="male" /><label
for="male"
>男</label
>
<input type="radio" name="gender" value="female" id="female" /><label
for="female"
>女</label
>
<input
type="radio"
name="gender"
value="secret"
id="secret"
checked
/>
<label for="secret">保密</label>
</div>
<div>
<!-- 复选框 -->
<label for="programe">爱好</label>
<!-- 因为复选框返回时一个或多个值,最方便后端用数组来处理.所以将name名称设置位数组形式便于后端脚本处理 -->
<input type="checkbox" name="hobby[]" id="game" value="game" /><label
for="game"
>打游戏</label
>
<input
type="checkbox"
name="hobby[]"
id="shoot"
value="shoot"
/><label for="shoot">摄影</label>
<input
type="checkbox"
name="hobby"
id="programe"
value="programe"
checked
/><label for="programe">编程</label>
</div>
<!-- 选项列表 -->
<div>
<label for="brand">手机:</label>
<input type="search" list="phone" name="brand" id="brand" />
<datalist id="phone">
<option value="apple"></option>
<option value="huawei">华为</option>
<option value="mi" label="小米"></option>
</datalist>
</div>
</fieldset>
<fieldset>
<legend>其他信息(选填)</legend>
<!-- 文件上传 -->
<div>
<label for="uploads">上传头像:</label>
<input
type="file"
name="user_pic"
id="uploads"
accept="image/png,image/jpeg,image/gif"
/>
</div>
<!-- 文本域 -->
<div>
<textarea
name="resume"
id="resume"
cols="30"
rows="10"
placeholder="不超过100个字"
></textarea>
</div>
</fieldset>
<!-- 隐藏域,例如用户id,注册,登录的时间... -->
<input type="hidden" name="user_id" value="123" />
<p>
<input type="button" value="提交" class="btn" />
<button class="btn">提交</button>
</p>
</form>
<script>
function ShowPwd(ele) {
document.querySelector("#pwd-1").type = "text";
}
</script>
</body>
</html>
效果预览: