博客列表 >表格实现购物车,表单实现注册页面实战

表格实现购物车,表单实现注册页面实战

小追
小追原创
2020年06月17日 20:56:22663浏览

1.用表格实现购物车清单

知识点:表格的常用元素

序号 元素 定义
1 <table></table> 表格主体
2 <caption></caption> 表格标题
3 <colgroup></colgroup> 对表格中的列进行格式化
4 <thead></thead> 表格头部
5 <th></th> 表头单元格
6 <tbody></tbody> 表格主体,一个表格中可以有多个主体
7 <tfoot></tfoot> 表格尾部
8 <tr></tr> 表格中的行
9 <td></td> 表格中的单元格

表格实现购物车清单代码:

  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. table{
  9. border-collapse:collapse;
  10. width:70%;
  11. margin:auto;
  12. color:#666;
  13. font-weight:lighter;
  14. text-align:center;
  15. }
  16. table thead th,table td{
  17. border-bottom:1px solid #ccc;
  18. padding:10px;
  19. }
  20. table caption{
  21. font-size:1.5rem;
  22. margin-bottom:10px;
  23. color:seagreen;
  24. }
  25. table th{
  26. font-weight:lighter;
  27. color:seagreen;
  28. }
  29. table thead{
  30. background-color:lightcyan;
  31. }
  32. /* 表格隔行变色 */
  33. table tbody tr:nth-of-type(even){
  34. background-color:#efef;
  35. }
  36. /* 鼠标悬停动效 */
  37. table tbody tr:hover{
  38. background-color:lightcyan;
  39. }
  40. table tfoot td{
  41. border-bottom:none;
  42. color:coral;
  43. font-size:1.2rem;
  44. font-weight:bolder;
  45. }
  46. body div{
  47. width:70%;
  48. margin:10px auto;
  49. }
  50. body div:first-of-type button{
  51. float:right;
  52. border:none;
  53. width:150px;
  54. height:38px;
  55. background-color:aquamarine;
  56. color:white;
  57. /* 设置鼠标样式 */
  58. cursor:pointer;
  59. }
  60. body div:first-of-type button:hover{
  61. background-color:coral;
  62. font-size:1.1rem;
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. <table>
  68. <caption>购物车</caption>
  69. <thead>
  70. <tr>
  71. <th>序号</th>
  72. <th>产品名称</th>
  73. <th>产品SKU</th>
  74. <th>单价/元</th>
  75. <th>规格</th>
  76. <th>数量</th>
  77. <th>金额/元</th>
  78. </tr>
  79. </thead>
  80. <tbody>
  81. <tr>
  82. <td>1</td>
  83. <td>中性笔</td>
  84. <td>Z-0001</td>
  85. <td>2</td>
  86. <td></td>
  87. <td>10</td>
  88. <td>20</td>
  89. </tr>
  90. <tr>
  91. <td>2</td>
  92. <td>圆珠笔</td>
  93. <td>Y-0001</td>
  94. <td>1</td>
  95. <td></td>
  96. <td>10</td>
  97. <td>10</td>
  98. </tr>
  99. <tr>
  100. <td>3</td>
  101. <td>铅笔</td>
  102. <td>Q-0001</td>
  103. <td>0.5</td>
  104. <td></td>
  105. <td>30</td>
  106. <td>15</td>
  107. </tr>
  108. <tr>
  109. <td>4</td>
  110. <td>画笔</td>
  111. <td>H-0001</td>
  112. <td>5</td>
  113. <td></td>
  114. <td>60</td>
  115. <td>300</td>
  116. </tr>
  117. </tbody>
  118. <tfoot>
  119. <tr>
  120. <td colspan="5">总计:</td>
  121. <td>110</td>
  122. <td>345</td>
  123. </tr>
  124. </tfoot>
  125. </table>
  126. <div>
  127. <button>结算</button>
  128. </div>
  129. </body>
  130. </html>

实现图:


2.表单实现注册页面

知识点:表单的基本元素

序号 元素 定义
1 <form></form> 表单主体内容
2 <fieldset></fieldset> 子集段,将表单分成内容段落
3 <legend></legend> 字集段标题
4 <label for="id"></label> 为input做标记
5 <input /> 用于收集用户信息
6 <datalist></datalist> 下拉搜索索引

表单实现注册页面代码:

  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. body{
  10. color:#555;
  11. }
  12. /* 给h3文字居中 */
  13. h3{
  14. text-align:center;
  15. }
  16. /* 表单主体样式 */
  17. form{
  18. width:450px;
  19. margin:30px auto;
  20. border-top:1px solid #aaa;
  21. }
  22. /* 表单fieldset的样式 */
  23. form fieldset{
  24. border:1px solid seagreen;
  25. background-color:lightcyan;
  26. box-shadow:2px 4px #bbb;
  27. border-radius:10px;
  28. margin:20px;
  29. }
  30. /* 设置fieldset元素下的所有legend元素的样式 */
  31. form fieldset legend{
  32. background-color:rgb(178,231,201);
  33. border-radius:10px;
  34. color:seagreen;
  35. padding:5px 15px;
  36. }
  37. /* 设置form元素下的所有div的样式 */
  38. form div{
  39. margin:5px;
  40. }
  41. /* 设置提交按钮的样式 */
  42. form .btn{
  43. width:80px;
  44. height:30px;
  45. border:none;
  46. background-color:seagreen;
  47. color:#ddd;
  48. float:right;
  49. }
  50. form .btn:hover{
  51. background-color:coral;
  52. color:white;
  53. cursor:pointer;
  54. }
  55. /* 设置输入框获得焦点的时候的样式 */
  56. input:focus{
  57. background-color:rgb(226,226,175);
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <h3>用户注册</h3>
  63. <form action="" method="POST">
  64. <fieldset>
  65. <legend>基本信息(必填)</legend>
  66. <div>
  67. <!-- label的for属性要和input的id属性绑定 -->
  68. <!-- 一个表单只能有一个自动获取焦点 -->
  69. <label for = "my-username">用户名:</label>
  70. <input type = "text"
  71. id = "my-username"
  72. name = "username"
  73. placeholder = "6-15位字符"
  74. autofocus
  75. required
  76. />
  77. </div>
  78. <div>
  79. <label for = "email-id">邮箱:</label>
  80. <input type = "email"
  81. id="email-id"
  82. name="email"
  83. placeholder = "z@haoai.cc"
  84. required
  85. />
  86. </div>
  87. <div>
  88. <label for = "pwd-1">密码:</label>
  89. <input type = "password"
  90. id = "pwd-1"
  91. name = "password1"
  92. placeholder = "不少于6位的字母+数字"
  93. />
  94. </div>
  95. <div>
  96. <label for = "pwd-2">确认:</label>
  97. <input type = "password"
  98. id="pwd-2"
  99. name = "password2"
  100. />
  101. </div>
  102. </fieldset>
  103. <fieldset>
  104. <legend>扩展信息(选填)</legend>
  105. <!-- 单选按钮 -->
  106. <div>
  107. <!-- 可以把label的for属性去掉,把input写到label中 -->
  108. <!-- <label>生日:
  109. <input type="date" name="birthday" />
  110. </label> -->
  111. <label for = "birthday">生日:</label>
  112. <input type="date" name="birthday" id="birthday" />
  113. </div>
  114. <div>
  115. <label for="secret">性别:</label>
  116. <input type="radio" name="gender" value="male" id="male" /><label for="male">男性</label>
  117. <input type="radio" name="gender" value="female" id="female" /> <label for="female">女性</label>
  118. <input type="radio" name="gender" value="secret" id="secret" checked /><label for="secret">保密</label>
  119. </div>
  120. <!-- 复选框 -->
  121. <!-- 因为复选框返回的是一个或多个值,后端用数组来处理最方便,所以将name名设置为数组形式 -->
  122. <div>
  123. <label for="programme">爱好</label>
  124. <input type="checkbox" name="hobby[]" id="game" value="game" /><label for="game">打游戏</label>
  125. <input type="checkbox" name="hobby[]" id="shoot" value="shoot" /><label for="shoot">摄影</label>
  126. <input type="checkbox" name="hobby[]" id="programme" value="programme" checked /><label for="programme">编程</label>
  127. </div>
  128. <div>
  129. <!-- 选项列表 -->
  130. <!-- input中的list属性值要和datalist的id属性值一致 -->
  131. <label for="brand">手机:</label>
  132. <input type="search" list="phone" name="brand" id="brand" />
  133. <datalist id="phone">
  134. <option value="apple"> </option>
  135. <option value="huawei" label="华为"></option>
  136. <option value="mi" label="小米"> </option>
  137. </datalist>
  138. </div>
  139. </fieldset>
  140. <fieldset>
  141. <legend>其他信息(选填)</legend>
  142. <!-- 文件上传 -->
  143. <div>
  144. <lable for="uploads">上传头像:</lable>
  145. <input type="file" name="user_pic" id="uploads" accept="imag/jpeg, imag.png, imag.gif" />
  146. </div>
  147. <!-- 文本域 -->
  148. <div>
  149. <label for="resume">简历:</label>
  150. <textarea name="resume" id="resume" cols="30" rows="5" placeholder="不超过100字"></textarea>
  151. </div>
  152. <!-- 隐藏域,用于记录用户注册、登录时间等信息,不用展示在浏览器显示页面中 -->
  153. <input type="hidden" name="user_id" value="123" />
  154. </fieldset>
  155. <button class="btn">提交</button>
  156. </form>
  157. </body>
  158. </html>

实现图:

" class="reference-link">

总结:表格和表单都是处理数据的工具

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