博客列表 >购物车与注册页面实战

购物车与注册页面实战

Haggi的糖果屋
Haggi的糖果屋原创
2020年06月21日 02:37:37617浏览

一、购物车实战

构成购物车表格的9个元素

  • <table></table>表格,数据化格式的工具
  • <caption></caption>表格标题
  • <colgroup></colgroup>对表格中的列进行组合
  • <thead></thead>表格头
  • <tbody></tbody>表格主体
  • <tfoot></tfoot>表格尾部
  • <tr></tr>行
  • <th></th>定义表格头的单元格
  • <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. html {
  9. font-size: 16px;
  10. }
  11. table {
  12. /* 将单元格之间的间隙去掉 */
  13. border-collapse: collapse;
  14. width: 90%;
  15. margin: auto;
  16. color: black;
  17. font-weight: lighter;
  18. text-align: center;
  19. /* position: absolute;
  20. top: 0;
  21. left: 0;
  22. right: 0;
  23. bottom: 0; */
  24. }
  25. /* 标题样式 */
  26. table caption {
  27. font-size: 1.5rem;
  28. font-weight: bolder;
  29. margin-bottom: 15px;
  30. color: red;
  31. text-align: left;
  32. }
  33. span {
  34. float: right;
  35. }
  36. input:focus {
  37. background-color: cornsilk;
  38. }
  39. /* 表格线直接添加到单元格上面 */
  40. table thead th,
  41. table td {
  42. border-bottom: 1px solid #ccc;
  43. padding: 10px;
  44. }
  45. table tbody {
  46. border-left: 1px solid #ccc;
  47. border-right: 1px solid #ccc;
  48. }
  49. table th {
  50. font-weight: lighter;
  51. }
  52. table thead tr:first-of-type {
  53. background-color: black;
  54. color: whitesmoke;
  55. font-size: larger;
  56. font-style: initial;
  57. font-weight: bold;
  58. }
  59. /* 隔行变色 */
  60. table tbody tr:nth-of-type(even) {
  61. background-color: lightgray;
  62. }
  63. /* 鼠标悬停效果 */
  64. table tbody tr:hover {
  65. background-color: lightblue;
  66. }
  67. /* 底部样式 */
  68. .col {
  69. text-align: left;
  70. padding-left: 6%;
  71. }
  72. table tfoot td {
  73. border-bottom: none;
  74. color: coral;
  75. font-size: 1.2rem;
  76. font-weight: bolder;
  77. text-align: center;
  78. }
  79. /* 结算按钮 */
  80. body div:last-of-type {
  81. width: 90%;
  82. }
  83. body div:first-of-type button {
  84. /* 靠右 */
  85. float: right;
  86. width: 100px;
  87. height: 50px;
  88. background-color: rgb(168, 8, 8);
  89. font-size: 22px;
  90. color: white;
  91. border-radius: 10%;
  92. /* 设置鼠标样式 */
  93. cursor: pointer;
  94. }
  95. body div:first-of-type button:hover {
  96. background-color: limegreen;
  97. font-size: 24px;
  98. }
  99. </style>
  100. </head>
  101. <body>
  102. <!-- 表格 -->
  103. <table>
  104. <!-- 标题 -->
  105. <caption>
  106. 购物车
  107. <span class="serach"
  108. ><input type="search" placeholder="商品" /><button>搜索</button></span
  109. >
  110. </caption>
  111. <!-- 头部 -->
  112. <thead>
  113. <tr>
  114. <th>序号</th>
  115. <th>商品</th>
  116. <th>价格</th>
  117. <th>单位</th>
  118. <th>数量</th>
  119. <th>金额</th>
  120. </tr>
  121. </thead>
  122. <!-- 主体 -->
  123. <tbody>
  124. <tr>
  125. <td>1</td>
  126. <td>绿茶</td>
  127. <td>100</td>
  128. <td></td>
  129. <td>1</td>
  130. <td>100</td>
  131. </tr>
  132. <tr>
  133. <td>2</td>
  134. <td>白茶</td>
  135. <td>100</td>
  136. <td></td>
  137. <td>1</td>
  138. <td>100</td>
  139. </tr>
  140. <tr>
  141. <td>3</td>
  142. <td>黄茶</td>
  143. <td>100</td>
  144. <td></td>
  145. <td>1</td>
  146. <td>100</td>
  147. </tr>
  148. <tr>
  149. <td>4</td>
  150. <td>青茶</td>
  151. <td>100</td>
  152. <td></td>
  153. <td>1</td>
  154. <td>100</td>
  155. </tr>
  156. <tr>
  157. <td>5</td>
  158. <td>红茶</td>
  159. <td>100</td>
  160. <td></td>
  161. <td>1</td>
  162. <td>100</td>
  163. </tr>
  164. <tr>
  165. <td>6</td>
  166. <td>黑茶</td>
  167. <td>100</td>
  168. <td></td>
  169. <td>1</td>
  170. <td>100</td>
  171. </tr>
  172. </tbody>
  173. <!-- 底部 -->
  174. <tfoot>
  175. <tr>
  176. <td class="col" colspan="4">总计:</td>
  177. <td id="sum"></td>
  178. <td id="sumtotal"></td>
  179. </tr>
  180. </tfoot>
  181. </table>
  182. <div>
  183. <button>结算</button>
  184. </div>
  185. </body>
  186. <script>
  187. //获取数量表格进行总计
  188. const num = document.querySelectorAll("tbody > tr > td:nth-child(5)");
  189. //获取金额表格进行总计
  190. const numtotal = document.querySelectorAll("tbody > tr > td:nth-child(6)");
  191. //定义数组
  192. var numarrA = new Array();
  193. var numarrB = new Array();
  194. //获取每行商品数量
  195. num.forEach(function (n, index) {
  196. var numtext = n.innerHTML;
  197. numarrA[index] = numtext;
  198. });
  199. //将字符串转为数字
  200. numarrB = numarrA.map(Number);
  201. //累加
  202. var sum = 0;
  203. for (var i = 0; i < numarrB.length; i++) {
  204. sum += numarrB[i];
  205. }
  206. var numarrC = new Array();
  207. var numarrD = new Array();
  208. numtotal.forEach(function (n, index) {
  209. var numtext = n.innerHTML;
  210. numarrC[index] = numtext;
  211. });
  212. numarrD = numarrC.map(Number);
  213. var sumtotal = 0;
  214. for (var i = 0; i < numarrB.length; i++) {
  215. sumtotal += numarrD[i];
  216. }
  217. //给“总计”所在表格添加监听
  218. const click1 = document.querySelector("tfoot > tr > td:first-child ");
  219. //点击后显示总的“数量”“金额”
  220. click1.addEventListener("click", function (ev) {
  221. document.getElementById("sum").innerHTML = sum;
  222. document.getElementById("sumtotal").innerHTML = sumtotal;
  223. });
  224. </script>
  225. </html>

运行图:

初始运行

点击“总计”


二、用户注册页面

基本表单元素
1.input标签
| 类型 | 描述 |
| —————— | —————— |
| type=”text” |文本类型 |
| type=”email” | 邮件类型 |
| type=”password” | 密码 |
| type=”date” | 时间日期 |
| type=”radio” |单选框 |
| type=”checkbox” |复选框 |
|type=”search | 搜索框 |
| type=”file” | 文件上传 |
|type=”hidden” | 隐藏选项 |
2.textarea标签(文本域)
<textarea></textarea>

3.form标签(表单)
<form action="" method=""></from>

4.filedset、legend标签(控件组)

  1. <fieldset>
  2. <legend>...</legend>
  3. .....
  4. </fieldset>

实战代码

  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. background-color: lightgoldenrodyellow;
  11. }
  12. h3 {
  13. text-align: center;
  14. }
  15. form {
  16. width: 450px;
  17. margin: 30px auto;
  18. border-top: 1px solid #aaa;
  19. border-radius: 5%;
  20. background-color: ghostwhite;
  21. }
  22. form fieldset {
  23. border: 1px solid lightgray;
  24. background-color: lightgrey;
  25. box-shadow: 2px 2px 4px #bbb;
  26. border-radius: 10px;
  27. margin: 20px;
  28. }
  29. form fieldset legend {
  30. background-color: gray;
  31. border-radius: 10px;
  32. color: wheat;
  33. padding: 5px 20px;
  34. text-align: center;
  35. font-size: 20px;
  36. }
  37. form div {
  38. margin: 10px;
  39. padding-left: 15%;
  40. font-weight: bold;
  41. }
  42. form p {
  43. text-align: center;
  44. }
  45. form .btn {
  46. width: 80px;
  47. height: 30px;
  48. border: none;
  49. background-color: lightskyblue;
  50. color: darkslategrey;
  51. border-radius: 5%;
  52. }
  53. form .btn:hover {
  54. background-color: red;
  55. color: white;
  56. cursor: pointer;
  57. border-radius: 5%;
  58. }
  59. input {
  60. border: 1px solid whitesmoke;
  61. border-radius: 20px;
  62. font-style: initial;
  63. }
  64. input:focus {
  65. background-color: rgb(226, 226, 175);
  66. }
  67. .cha {
  68. display: none;
  69. }
  70. .ch {
  71. display: block;
  72. }
  73. .btn {
  74. margin-bottom: 10px;
  75. }
  76. </style>
  77. </head>
  78. <body>
  79. <h3>用户注册</h3>
  80. <!-- form+input.... -->
  81. <form action="" method="POST">
  82. <!-- 控件组 -->
  83. <fieldset>
  84. <legend>基本信息(必填)</legend>
  85. <div>
  86. <label for="my-username">账号:</label>
  87. <input
  88. type="text"
  89. id="my-username"
  90. name="username"
  91. placeholder="不少于6位字符"
  92. autofocus
  93. />
  94. </div>
  95. <div>
  96. <label for="email-id">邮箱:</label>
  97. <input
  98. type="email"
  99. name="email"
  100. id="email-id"
  101. placeholder="12345678@qq.com"
  102. />
  103. </div>
  104. <!-- 密码 -->
  105. <div>
  106. <label for="pwd-2">密码:</label>
  107. <input
  108. type="password"
  109. name="password1"
  110. id="pwd-2"
  111. placeholder="********"
  112. />
  113. </div>
  114. <div>
  115. <label for="pwd-1">确认:</label>
  116. <input
  117. type="password"
  118. name="password2"
  119. id="pwd-1"
  120. placeholder="********"
  121. />
  122. </div>
  123. <div>
  124. <label class="change2" style="float: right; color: lightslategray;"
  125. >扩展信息</label
  126. >
  127. </div>
  128. </fieldset>
  129. <!-- 扩展信息(选填) -->
  130. <fieldset class="cha">
  131. <legend>扩展信息(选填)</legend>
  132. <div>
  133. <label
  134. >生日:
  135. <input type="date" name="birthday" style="width: 166px;" />
  136. </label>
  137. </div>
  138. <div>
  139. <!-- 单选按钮 -->
  140. <label for="female">性别:</label>
  141. <!-- 单选按钮中的name属性名必须相同 -->
  142. <input
  143. type="radio"
  144. name="gender"
  145. value="male"
  146. id="male"
  147. checked
  148. /><label for="male"></label>
  149. <input type="radio" name="gender" value="female" id="female" /><label
  150. for="female"
  151. ></label
  152. >
  153. </div>
  154. <div>
  155. <!-- 复选框 -->
  156. <label for="programme">爱好</label>
  157. <!-- 因为复选框返回是一个或多个值,最方便后端用数组来处理, 所以将name名称设置为数组形式便于后端脚本处理 -->
  158. <input
  159. type="checkbox"
  160. name="hobby[]"
  161. id="game"
  162. value="game"
  163. checked
  164. /><label for="game">打游戏</label>
  165. <input
  166. type="checkbox"
  167. name="hobby[]"
  168. value="shoot"
  169. id="shoot"
  170. /><label for="shoot">摄影</label>
  171. <input
  172. type="checkbox"
  173. name="hobby[]"
  174. value="programme"
  175. id="programme"
  176. /><label for="programme">编程</label>
  177. </div>
  178. <div>
  179. <!-- 选项列表 -->
  180. <label for="brand">手机:</label>
  181. <input type="search" list="phone" name="brand" id="brand" />
  182. <datalist id="phone">
  183. <option value="苹果" label="11"> </option>
  184. <option value="华为" label="mate30"></option>
  185. <option value="小米" label="10"> </option>
  186. </datalist>
  187. </div>
  188. <div>
  189. <label for="uploads">上传头像:</label>
  190. <input
  191. type="file"
  192. name="user_pic"
  193. id="uploads"
  194. accept="image/png, image/jpeg, image/gif"
  195. />
  196. </div>
  197. <!--文本域-->
  198. <div>
  199. <label for="resume">简历:</label>
  200. <!--注意文本域没有value属性-->
  201. <textarea
  202. name="resume"
  203. id="resume"
  204. cols="30"
  205. rows="5"
  206. placeholder="不超过100字"
  207. ></textarea>
  208. </div>
  209. </fieldset>
  210. <!-- 隐藏用户的Id等一些不用显示的数据 -->
  211. <input type="hidden" name="user_id" value="123" />
  212. <p>
  213. <button class="btn">注册</button>
  214. </p>
  215. </form>
  216. </body>
  217. <script>
  218. //获取需显示表单所在标签
  219. const change1 = document.querySelectorAll(".cha");
  220. //获取扩展信息所在标签
  221. const change2 = document.querySelector(".change2");
  222. //添加点击事件
  223. change2.addEventListener("click", function (ev) {
  224. //改变扩展信息表单的属性名
  225. change1[0].className = "ch";
  226. });
  227. </script>
  228. </html>

结果图

初始结果图:

点击“扩展信息”后


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