博客列表 >HTML 表格表单实战练习小结

HTML 表格表单实战练习小结

JKY辉哥
JKY辉哥原创
2020年06月17日 16:10:222388浏览

1.表格基础知识

  • 1.1数据标签:
序号 标签 描述
1 <table> 定义表格, 必选
2 <tr> 定义表格中的行, 必选
3 <th> 定义表格头部中的单元格, 必选
4 <td> 定义表格主体中的单元格, 必选
  • 1.2结构标签:
序号 标签 描述
1 <option> 定义表格标题, 可选
2 <thead> 定义表格头格, 只需定义一次, 可选
3 <tbody> 定义表格主体, 允许定义多次, 可选
4 <tfooter> 定义表格底, 只需定义一次, 可选
  • 1.3常用属性:
序号 属性 所属标签 描述
1 border <table> 添加表格框
2 cellpadding <table> 设置单元格内边距
2 cellspacing <table> 设置单元格边框间隙
2 align 不限 设置单元格内容水平居中
2 bgcolor 不限 设置背景色

2.表格制作购物车实战页面

  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. /* 设置表格字体大小 */
  9. table {
  10. font-size: 14px;
  11. }
  12. /* 将单元格之间间隙去掉 */
  13. table {
  14. border-collapse: collapse;
  15. width: 70%;
  16. margin: auto;
  17. color: #888;
  18. font-size: larger;
  19. text-align: center;
  20. }
  21. td,
  22. th {
  23. border-bottom: 1px solid #ccc;
  24. padding: 10px;
  25. }
  26. /* 标题样式 */
  27. table caption {
  28. font-size: 1.5rem;
  29. color: yellowgreen;
  30. margin-bottom: 20px;
  31. }
  32. table th {
  33. font-weight: bolder;
  34. color: white;
  35. }
  36. table thead tr:first-of-type {
  37. background-color: teal;
  38. }
  39. /* 隔行变色 */
  40. table tbody tr:nth-of-type(even) {
  41. background-color: yellowgreen;
  42. color: white;
  43. }
  44. /* 鼠标悬停效果 */
  45. table tbody tr:hover {
  46. background-color: violet;
  47. color: black;
  48. }
  49. /* 表格底部样式 */
  50. table tfoot td {
  51. border-bottom: none;
  52. color: red;
  53. font-size: 1.2rem;
  54. }
  55. /* 结算按钮 */
  56. body div:last-of-type {
  57. width: 70%;
  58. margin: 10px 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: sandybrown;
  73. font-size: 1.1rem;
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <!-- 表格 -->
  79. <table>
  80. <!-- 标题 -->
  81. <caption>
  82. 购物车
  83. </caption>
  84. <!-- 表格头部 -->
  85. <thead>
  86. <tr>
  87. <th>ID</th>
  88. <th>品名</th>
  89. <th>单价/元</th>
  90. <th>单位</th>
  91. <th>数量</th>
  92. <th>金额/元</th>
  93. </tr>
  94. </thead>
  95. <!-- 表格主体 -->
  96. <tbody>
  97. <tr>
  98. <td>SN-1010</td>
  99. <td>MacBook Pro电脑</td>
  100. <td>18999</td>
  101. <td></td>
  102. <td>1</td>
  103. <td>18999</td>
  104. </tr>
  105. <tr>
  106. <td>SN-1020</td>
  107. <td>iPhone手机</td>
  108. <td>4999</td>
  109. <td></td>
  110. <td>2</td>
  111. <td>9998</td>
  112. </tr>
  113. <tr>
  114. <td>SN-1030</td>
  115. <td>智能AI音箱</td>
  116. <td>399</td>
  117. <td></td>
  118. <td>5</td>
  119. <td>1995</td>
  120. </tr>
  121. <tr>
  122. <td>SN-1040</td>
  123. <td>SSD移动硬盘</td>
  124. <td>888</td>
  125. <td></td>
  126. <td>2</td>
  127. <td>1776</td>
  128. </tr>
  129. <tr>
  130. <td>SN-1050</td>
  131. <td>黄山毛峰</td>
  132. <td>999</td>
  133. <td></td>
  134. <td>3</td>
  135. <td>2997</td>
  136. </tr>
  137. </tbody>
  138. <!-- 表格尾部 -->
  139. <tfoot>
  140. <td colspan="4">总计:</td>
  141. <td>13</td>
  142. <td>35765</td>
  143. </tfoot>
  144. </table>
  145. <!-- 结算按钮 -->
  146. <div>
  147. <button>结算</button>
  148. </div>
  149. </body>
  150. </html>

运行结果截图:
表格制作购物车页面


3.表单

  • 3.1 表单基本元素

序号 元素 名称 描述
1 <form> 表单容器 表单应该放在该标签内提交
2 <input> 输入控件 type属性指定控件类型
3 <label> 控件标签 用于控件功能描述与内容关联
4 <select> 下拉列表 用于选择预置的输入内容
5 <datalist> 预置列表 用于展示预置的输入内容
6 <option> 预置选项 <select>,<datalist>配合
7 <textarea> 文本域 多行文本框,常与富文本编辑器配合
8 <button> 按钮 用于提交表单

表单元素的公共属性(并非所有元素都具备)

序号 属性 描述
1 name 元素/控件名称,用做服务器端脚本的变量名称
2 value 提交到服务器端的数据
3 placeholder 输入框的提示信息
4 form 所属的表单,与<form name="">对应
5 autofocus 页面加载时,自动获取焦点
6 required 必填 / 必选项
7 readonly 该控件内容只读
8 disabled 是否禁用

  • 3.2.表单制作一个用户注册页面

  • html 代码

  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. <body>
  9. <h3>用户注册</h3>
  10. <!-- form+input -->
  11. <form action="" method="POST">
  12. <fieldset>
  13. <legend>基本信息(必填)</legend>
  14. <div>
  15. <label for="username">账号:</label>
  16. <input
  17. type="text"
  18. id="my-username"
  19. name="username"
  20. placeholder="6-15位字符"
  21. />
  22. </div>
  23. <div>
  24. <label for="email-id">邮箱:</label>
  25. <input
  26. type="email"
  27. name="email"
  28. id="email-id"
  29. placeholder="demo@qq.com"
  30. required
  31. />
  32. </div>
  33. <!-- 密码 框-->
  34. <div>
  35. <label for="pwd-1">密码:</label>
  36. <input
  37. type="password"
  38. name="password1"
  39. id="pwd-2"
  40. placeholder="不少于6位且字母+数字"
  41. />
  42. <!-- 点击显示密码 -->
  43. <!-- <button onclick="ShowPsw()" id="btn" type="button">显示密码</button> -->
  44. </div>
  45. <div>
  46. <label for="pwd-2">确认:</label>
  47. <input type="password" name="password2" id="pwd-1" />
  48. </div>
  49. </fieldset>
  50. <fieldset>
  51. <legend>扩展信息(选填)</legend>
  52. <div>
  53. <label
  54. >生日:
  55. <input type="date" name="birthday" />
  56. </label>
  57. </div>
  58. <!-- 单选按钮 -->
  59. <div>
  60. <label for="secret">性别:</label>
  61. <!-- 单选按钮中在name属性名必须相同 -->
  62. <input type="radio" value="male" name="gender" id="male" /><label
  63. for=""
  64. ></label
  65. >
  66. <input type="radio" value="female" name="gender" id="female" /><label
  67. for=""
  68. ></label
  69. >
  70. <input
  71. type="radio"
  72. value="secret"
  73. name="gender"
  74. id="secret"
  75. checked
  76. /><label for="secret">保密</label>
  77. </div>
  78. <!-- 复选框 -->
  79. <div>
  80. <label for="programme">爱好</label>
  81. <!-- 因为复选框返回是一个或多个值,最方便后端用数组来处理, 所以将name名称设置为数组形式便于后端脚本处理 -->
  82. <input type="checkbox" name="hobby[]" id="game" value="game" />
  83. <label for="game">打游戏</label>
  84. <input type="checkbox" name="hobby[]" id="shoot" value="shoot" />
  85. <label for="game">编程</label>
  86. <input
  87. type="checkbox"
  88. name="hobby[]"
  89. id="programme"
  90. value="programme"
  91. checked
  92. />
  93. <label for="game">编程</label>
  94. </div>
  95. <!-- 选项列表 -->
  96. <div>
  97. <label for="brand">手机:</label>
  98. <input type="search" list="phone" id="brand" name="brand" />
  99. <datalist id="phone">
  100. <option value="apple"> </option>
  101. <option value="huawei">华为</option>
  102. <option value="mi" label="小米"> </option>
  103. </datalist>
  104. </div>
  105. </fieldset>
  106. <fieldset>
  107. <legend>其它信息(选填)</legend>
  108. <!--文件上传-->
  109. <div>
  110. <label for="uploads">上传头像:</label>
  111. <input
  112. type="file"
  113. name="user_pic"
  114. id="uploads"
  115. accept="image/png, image/jpeg, image/gif"
  116. />
  117. </div>
  118. <!--文本域-->
  119. <div>
  120. <label for="resume">简历:</label>
  121. <!--注意文本域没有value属性-->
  122. <textarea
  123. name="resume"
  124. id="resume"
  125. cols="30"
  126. rows="5"
  127. placeholder="不超过100字"
  128. ></textarea>
  129. </div>
  130. </fieldset>
  131. <!-- 隐藏域, 例如用户的Id, 注册,登录的时间,无需用户填写 -->
  132. <input type="hidden" name="user_id" value="123" />
  133. <p>
  134. <input type="submit" value="提交" class="btn" />
  135. <button class="btn">提交</button>
  136. </p>
  137. </form>
  138. <script>
  139. //输入密码,点击名文显示
  140. // function ShowPsw(ele) {
  141. // const pwd = document.querySelector("#pwd-2");
  142. // pwd.type = "text";
  143. // 代码写成一行
  144. // document.querySelector("#pwd-2").type = "text";
  145. // }
  146. </script>
  147. </body>
  148. </html>

运行结果截图
表格制作购物车页面

  • 3.3 添加 CSS 样式代码后

  1. <style>
  2. body {
  3. color: #555;
  4. }
  5. h3 {
  6. text-align: center;
  7. }
  8. form {
  9. width: 450px;
  10. margin: 30px auto;
  11. border-top: 1px solid #aaa;
  12. }
  13. form fieldset {
  14. border: 1px solid seagreen;
  15. background-color: lightcyan;
  16. box-shadow: 2px 2px 4px #bbb;
  17. border-radius: 10px;
  18. margin: 20px;
  19. }
  20. form fieldset legend {
  21. background-color: rgb(178, 231, 201);
  22. border-radius: 10px;
  23. color: seagreen;
  24. padding: 5px 15px;
  25. }
  26. form div {
  27. margin: 5px;
  28. }
  29. form p {
  30. text-align: center;
  31. }
  32. form .btn {
  33. width: 80px;
  34. height: 30px;
  35. border: none;
  36. background-color: seagreen;
  37. color: #ddd;
  38. }
  39. form .btn:hover {
  40. background-color: coral;
  41. color: white;
  42. cursor: pointer;
  43. }
  44. input:focus {
  45. background-color: rgb(226, 226, 175);
  46. }
  47. </style>

运行结果截图
表格制作购物车页面

4.总结

  • 表格是数据格式化的工具
  • 完整表格 9 个标签:( table + caption + colgroup + thead + tbody + tfoot + tr + th + td)
  • 表格常用 7 个标签:table+capiton+thead+tbody+tr+th+tr+td
  • 最简单表格要用的 6 个标签:table+caption+tbody+tr+th/td
  • 表单是网站页面与用户之间实现互动一种工具,通常用来实现用户的注册登陆,信息收集
  • 表单元素添加disabled禁用属性的字段数据不会被提交,但是readonly只读属性的字段允许提交
  • 表单元素隐藏域的内容不会在 HTML 页面中显示,但是value属性的数据会被提交
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议