博客列表 >了解表格与表单并用表格实现购物车与表单实现用户注册页面

了解表格与表单并用表格实现购物车与表单实现用户注册页面

忠于原味
忠于原味原创
2020年06月17日 23:50:14693浏览

一.了解表格与表单

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. <input>type类型

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属性的数据会被提交

二.表格实现购物车

实例代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>表格实战:购物车</title>
  7. </head>
  8. <style>
  9. html {
  10. font-size: 14px;
  11. }
  12. /* 将单元格之间的间隙去掉 */
  13. table {
  14. border-collapse: collapse;
  15. width: 70%;
  16. margin: auto;
  17. color: #666;
  18. font-weight: lighter;
  19. text-align: center;
  20. }
  21. /* 表格线直接添加到单元格上面 */
  22. th,
  23. td {
  24. border-bottom: 1px solid #ccc;
  25. padding: 10px;
  26. }
  27. /* 标题样式 */
  28. table caption {
  29. font-size: 1.5rem;
  30. margin-bottom: 15px;
  31. font-weight: bolder;
  32. color: green;
  33. }
  34. table th {
  35. font-weight: lighter;
  36. color: green;
  37. }
  38. table thead tr:first-of-type {
  39. background-color: lightcyan;
  40. }
  41. /* 隔行变色 */
  42. table tbody tr:nth-of-type(even) {
  43. background-color: yellow;
  44. }
  45. /* 鼠标悬停效果 */
  46. table tbody tr:hover {
  47. background-color: pink;
  48. }
  49. table tfoot td {
  50. border-bottom: none;
  51. color: coral;
  52. font-size: 1.2rem;
  53. font-weight: bolder;
  54. }
  55. /* 结束按钮 */
  56. body div:first-of-type {
  57. width: 70%;
  58. margin: auto;
  59. }
  60. body div:first-of-type button {
  61. /* 靠右 */
  62. float: right;
  63. width: 120px;
  64. height: 32px;
  65. background-color: seagreen;
  66. color: white;
  67. border: none;
  68. /* 设置鼠标样式 */
  69. cursor: pointer;
  70. }
  71. body div:first-of-type button:hover {
  72. background-color: coral;
  73. font-size: 1.1rem;
  74. }
  75. </style>
  76. <body>
  77. <!-- 表格 -->
  78. <table>
  79. <!-- 标题 -->
  80. <caption>
  81. 购物车
  82. </caption>
  83. <!-- 头部 -->
  84. <thead>
  85. <tr>
  86. <th>ID</th>
  87. <th>品名</th>
  88. <th>单价/元</th>
  89. <th>单位</th>
  90. <th>数量</th>
  91. <th>金额</th>
  92. </tr>
  93. </thead>
  94. <!-- 主体 -->
  95. <tbody>
  96. <tr>
  97. <td>SN-1010</td>
  98. <td>MacBookpro 电脑</td>
  99. <td>18999</td>
  100. <td></td>
  101. <td>1</td>
  102. <td>18999</td>
  103. </tr>
  104. <tr>
  105. <td>SN-1020</td>
  106. <td>iPhone手机</td>
  107. <td>4999</td>
  108. <td></td>
  109. <td>2</td>
  110. <td>9998</td>
  111. </tr>
  112. <tr>
  113. <td>SN-1030</td>
  114. <td>智能AI音箱</td>
  115. <td>399</td>
  116. <td></td>
  117. <td>5</td>
  118. <td>1995</td>
  119. </tr>
  120. <tr>
  121. <td>SN-1040</td>
  122. <td>SSD移动硬盘</td>
  123. <td>888</td>
  124. <td></td>
  125. <td>2</td>
  126. <td>1776</td>
  127. </tr>
  128. <tr>
  129. <td>SN-1050</td>
  130. <td>黄山毛峰</td>
  131. <td>999</td>
  132. <td></td>
  133. <td>3</td>
  134. <td>2997</td>
  135. </tr>
  136. </tbody>
  137. <!-- 底部 -->
  138. <tfoot>
  139. <tr>
  140. <td colspan="4">总计:</td>
  141. <td>13</td>
  142. <td>35765</td>
  143. </tr>
  144. </tfoot>
  145. </table>
  146. <!-- 结算按钮 -->
  147. <div><button>结算</button></div>
  148. </body>
  149. </html>

效果预览:

" class="reference-link">

三.表单实现用户注册页面

实例代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>基本表单元素</title>
  7. <style>
  8. body {
  9. color: #555;
  10. }
  11. h3 {
  12. text-align: center;
  13. }
  14. form {
  15. width: 450px;
  16. margin: 30px auto;
  17. border-top: 1px solid #aaa;
  18. }
  19. form fieldset {
  20. border: 1px solid seagreen;
  21. background-color: lightcyan;
  22. box-shadow: 2px 2px 4px #bbb;
  23. border-radius: 10px;
  24. margin: 20px;
  25. }
  26. form fieldset legend {
  27. background-color: rgb(178, 231, 201);
  28. border-radius: 10px;
  29. color: seagreen;
  30. padding: 5px 15px;
  31. }
  32. form div {
  33. margin: 5px;
  34. }
  35. form p {
  36. text-align: center;
  37. }
  38. form .btn {
  39. width: 80px;
  40. height: 30px;
  41. border: none;
  42. background-color: seagreen;
  43. color: #ddd;
  44. }
  45. form .btn:hover {
  46. background-color: coral;
  47. color: white;
  48. cursor: pointer;
  49. }
  50. input:focus {
  51. background-color: rgb(226, 226, 175);
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <h3>用户注册</h3>
  57. <form action="" method="POST">
  58. <!-- 1.控件组 -->
  59. <fieldset>
  60. <legend>基本信息(必填)</legend>
  61. <div>
  62. <label for="my-username">账号:</label>
  63. <input
  64. type="text"
  65. name="username"
  66. id="my-username"
  67. placeholder="6到15位字符"
  68. autofocus
  69. required
  70. />
  71. </div>
  72. <div>
  73. <label for="email-id">邮箱:</label>
  74. <input
  75. type="email"
  76. name="email"
  77. id="email-id"
  78. placeholder="demo@qq.com"
  79. required
  80. />
  81. </div>
  82. <!-- 密码 -->
  83. <div>
  84. <label for="pwd-1">密码:</label>
  85. <input
  86. type="password"
  87. name="password1"
  88. id="pwd-1"
  89. placeholder="不少于6位且字母+数字"
  90. required
  91. />
  92. <button onclick="ShowPwd()" id="btn" type="button">显示密码</button>
  93. </div>
  94. <!-- 确认密码 -->
  95. <div>
  96. <label for="pwd-2">确认:</label>
  97. <input type="password" name="password2" id="pwd-2" required />
  98. </div>
  99. </fieldset>
  100. <fieldset>
  101. <legend>扩展信息(选填)</legend>
  102. <div>
  103. <label
  104. >生日:
  105. <input type="date" name="birthday" />
  106. </label>
  107. </div>
  108. <div>
  109. <!-- 单选按钮 -->
  110. <label for="">性别:</label>
  111. <!-- 单选按钮中的name属性必须相同 -->
  112. <input type="radio" name="gender" value="male" id="male" /><label
  113. for="male"
  114. ></label
  115. >
  116. <input type="radio" name="gender" value="female" id="female" /><label
  117. for="female"
  118. ></label
  119. >
  120. <input
  121. type="radio"
  122. name="gender"
  123. value="secret"
  124. id="secret"
  125. checked
  126. />
  127. <label for="secret">保密</label>
  128. </div>
  129. <div>
  130. <!-- 复选框 -->
  131. <label for="programe">爱好</label>
  132. <!-- 因为复选框返回时一个或多个值,最方便后端用数组来处理.所以将name名称设置位数组形式便于后端脚本处理 -->
  133. <input type="checkbox" name="hobby[]" id="game" value="game" /><label
  134. for="game"
  135. >打游戏</label
  136. >
  137. <input
  138. type="checkbox"
  139. name="hobby[]"
  140. id="shoot"
  141. value="shoot"
  142. /><label for="shoot">摄影</label>
  143. <input
  144. type="checkbox"
  145. name="hobby"
  146. id="programe"
  147. value="programe"
  148. checked
  149. /><label for="programe">编程</label>
  150. </div>
  151. <!-- 选项列表 -->
  152. <div>
  153. <label for="brand">手机:</label>
  154. <input type="search" list="phone" name="brand" id="brand" />
  155. <datalist id="phone">
  156. <option value="apple"></option>
  157. <option value="huawei">华为</option>
  158. <option value="mi" label="小米"></option>
  159. </datalist>
  160. </div>
  161. </fieldset>
  162. <fieldset>
  163. <legend>其他信息(选填)</legend>
  164. <!-- 文件上传 -->
  165. <div>
  166. <label for="uploads">上传头像:</label>
  167. <input
  168. type="file"
  169. name="user_pic"
  170. id="uploads"
  171. accept="image/png,image/jpeg,image/gif"
  172. />
  173. </div>
  174. <!-- 文本域 -->
  175. <div>
  176. <textarea
  177. name="resume"
  178. id="resume"
  179. cols="30"
  180. rows="10"
  181. placeholder="不超过100个字"
  182. ></textarea>
  183. </div>
  184. </fieldset>
  185. <!-- 隐藏域,例如用户id,注册,登录的时间... -->
  186. <input type="hidden" name="user_id" value="123" />
  187. <p>
  188. <input type="button" value="提交" class="btn" />
  189. <button class="btn">提交</button>
  190. </p>
  191. </form>
  192. <script>
  193. function ShowPwd(ele) {
  194. document.querySelector("#pwd-1").type = "text";
  195. }
  196. </script>
  197. </body>
  198. </html>

效果预览:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议