博客列表 >表格与表单学习

表格与表单学习

遇见
遇见原创
2020年06月18日 17:19:21579浏览

1.表格的学习,以购物车为例,详细如下:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>购物车</title>
  7. <style type="text/css">
  8. /* 浏览器默认外边距8像素,全部初始化0 */
  9. * {
  10. padding: 0;
  11. margin: 0;
  12. }
  13. .table {
  14. width: 70%;
  15. /* 表格边框线实心,去掉间隙 */
  16. border-collapse: collapse;
  17. /* 表格文字水平居中对齐 */
  18. text-align: center;
  19. /* 表格水平居中对齐 */
  20. margin: auto;
  21. }
  22. .table caption {
  23. /* 标题上下外边距10像素,左右自动,也就是水平居中 */
  24. margin: 10px auto;
  25. font-size: 20px;
  26. }
  27. .table tr th,
  28. .table tr td {
  29. border-bottom: 1px solid #efef;
  30. /* 字体显示更细 */
  31. font-weight: lighter;
  32. /* 表格内容内边距10像素 */
  33. padding: 10px;
  34. }
  35. /* 表格头部第一个设置背景颜色 */
  36. .table thead tr:first-child {
  37. background: lightgreen;
  38. }
  39. /* 表格内容主题,分组后偶数行显示背景颜色,也就是表格隔行换色 */
  40. .table tbody tr:nth-of-type(even) {
  41. background: #eeeeee;
  42. }
  43. /* 光标移到表格行上后,表格背景颜色改变 */
  44. .table tbody tr:hover {
  45. background: palegoldenrod;
  46. }
  47. .table tfoot {
  48. font-size: 18px;
  49. color: coral;
  50. }
  51. /* body内容分组后,第一个div设置宽度和外边距 */
  52. body div:first-of-type {
  53. width: 70%;
  54. /* 外边距:上10像素,下0像素,左右边距自动 */
  55. margin: 10px auto 0px;
  56. }
  57. button {
  58. width: 90px;
  59. height: 35px;
  60. /* 这个是圆角样式 */
  61. border-radius: 4px;
  62. color: #ffffff;
  63. background: darkcyan;
  64. /* 这个是去掉点击后的外边框虚线 */
  65. outline: none;
  66. float: right;
  67. }
  68. /* 光标移上去样式 */
  69. button:hover {
  70. /* 光标移上去后,改变光标形状,pointer表示小手形状 */
  71. cursor: pointer;
  72. background: darkolivegreen;
  73. font-size: 18px;
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <table class="table">
  79. <caption>
  80. 购物车
  81. </caption>
  82. <thead>
  83. <tr>
  84. <th>商品编码</th>
  85. <th>商品名称</th>
  86. <th>数量</th>
  87. <th>单价/元</th>
  88. <th>金额</th>
  89. </tr>
  90. </thead>
  91. <tbody>
  92. <tr>
  93. <td>NO2020061801</td>
  94. <td>小米笔记本</td>
  95. <td>1</td>
  96. <td>7899.00</td>
  97. <td>7899.00</td>
  98. </tr>
  99. <tr>
  100. <td>NO2020061802</td>
  101. <td>鼠标垫</td>
  102. <td>2</td>
  103. <td>8.00</td>
  104. <td>16.00</td>
  105. </tr>
  106. <tr>
  107. <td>NO2020061803</td>
  108. <td>毛峰茶</td>
  109. <td>1</td>
  110. <td>20.00</td>
  111. <td>20.00</td>
  112. </tr>
  113. <tr>
  114. <td>NO2020061804</td>
  115. <td>农夫山泉</td>
  116. <td>1</td>
  117. <td>2.00</td>
  118. <td>2.00</td>
  119. </tr>
  120. </tbody>
  121. <tfoot>
  122. <tr>
  123. <!-- colspan表示横向合并单元格,rowspan表示纵向合并单元格,后面跟上合并单元格数量,并且删除对应数量的单元格 -->
  124. <td colspan="2">合计:</td>
  125. <td>5</td>
  126. <td> </td>
  127. <td>7937.00</td>
  128. </tr>
  129. </tfoot>
  130. </table>
  131. <div>
  132. <button>结算</button>
  133. </div>
  134. </body>
  135. </html>

运行结果截图:

2.表单学习:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>提交表单</title>
  7. <style type="text/css">
  8. /* 浏览器默认外边距8像素,全部初始化0 */
  9. * {
  10. padding: 0;
  11. margin: 0;
  12. }
  13. input,
  14. textarea {
  15. outline: none;
  16. padding: 0 10px;
  17. border-radius: 5px;
  18. border: none;
  19. }
  20. textarea {
  21. line-height: 25px;
  22. }
  23. input[type="text"],
  24. input[type="password"],
  25. input[type="email"] {
  26. height: 25px;
  27. line-height: 25px;
  28. }
  29. input[type="radio"],
  30. input[type="checkbox"] {
  31. margin-left: 10px;
  32. margin-right: 5px;
  33. }
  34. input[type="button"] {
  35. background: mediumspringgreen;
  36. color: navy;
  37. padding: 5px;
  38. cursor: pointer;
  39. }
  40. .regbox {
  41. height: 100vh;
  42. display: flex;
  43. justify-content: center;
  44. align-items: center;
  45. font-weight: lighter;
  46. }
  47. .applyBox {
  48. width: 500px;
  49. color: #666;
  50. }
  51. .applyBox .title {
  52. text-align: center;
  53. font-size: 20px;
  54. margin: 10px 0px;
  55. }
  56. .forminfo fieldset {
  57. font-size: 14px;
  58. padding: 10px;
  59. margin-bottom: 10px;
  60. border-radius: 10px;
  61. background: linen;
  62. }
  63. .forminfo fieldset:last-of-type {
  64. margin-bottom: 0px;
  65. }
  66. .forminfo fieldset div:not(:last-of-type) {
  67. margin-bottom: 10px;
  68. }
  69. .forminfo fieldset div label:first-of-type {
  70. width: 80px;
  71. display: inline-block;
  72. }
  73. .applyBox .btnBox {
  74. text-align: center;
  75. }
  76. .btnBox .submitBtn {
  77. width: 90px;
  78. height: 35px;
  79. /* 这个是圆角样式 */
  80. border-radius: 4px;
  81. color: #ffffff;
  82. background: darkcyan;
  83. /* 这个是去掉点击后的外边框虚线 */
  84. outline: none;
  85. margin-top: 10px;
  86. }
  87. /* 光标移上去样式 */
  88. .btnBox .submitBtn:hover {
  89. /* 光标移上去后,改变光标形状,pointer表示小手形状 */
  90. cursor: pointer;
  91. background: darkolivegreen;
  92. font-size: 18px;
  93. }
  94. </style>
  95. </head>
  96. <body>
  97. <div class="regbox">
  98. <form action="">
  99. <div class="applyBox">
  100. <div class="title">用户注册</div>
  101. <div class="forminfo">
  102. <fieldset>
  103. <legend>基本信息(必填)</legend>
  104. <div>
  105. <!-- for的值要与input的id对应,这样点击lable也能直接点中input -->
  106. <!-- required只在submit提交表单时生效,可以提示必录 -->
  107. <!-- autofocus默认光标选中 -->
  108. <label for="userName">用户名称:</label>
  109. <input
  110. type="text"
  111. id="userName"
  112. name="userName"
  113. placeholder="请输入用户名"
  114. required
  115. autofocus
  116. />
  117. </div>
  118. <div>
  119. <label for="email">邮箱号:</label>
  120. <input
  121. type="email"
  122. id="email"
  123. name="email"
  124. placeholder="请输入邮箱号"
  125. required
  126. />
  127. </div>
  128. <div>
  129. <label for="password">密码:</label>
  130. <input
  131. type="password"
  132. id="password"
  133. name="password"
  134. placeholder="******"
  135. required
  136. />
  137. <input type="button" data-showid="password" value="显示密码" />
  138. </div>
  139. <div>
  140. <label for="confitmPwd">确认密码:</label>
  141. <input
  142. type="password"
  143. id="confitmPwd"
  144. name="confitmPwd"
  145. placeholder="******"
  146. required
  147. />
  148. <input
  149. type="button"
  150. data-showid="confitmPwd"
  151. value="显示密码"
  152. />
  153. </div>
  154. </fieldset>
  155. <fieldset>
  156. <legend>扩展信息(选填)</legend>
  157. <div>
  158. <label for="birthday">生日:</label>
  159. <input type="date" id="birthday" name="birthday" />
  160. </div>
  161. <div>
  162. <label for="gender">性别:</label>
  163. <input type="radio" id="male" name="sex" value="0" /><label
  164. for="male"
  165. ></label
  166. >
  167. <input type="radio" id="female" name="sex" value="1" /><label
  168. for="female"
  169. ></label
  170. >
  171. <input type="radio" id="gender" name="sex" value="2" /><label
  172. for="gender"
  173. >保密</label
  174. >
  175. </div>
  176. <div>
  177. <label for="read">爱好:</label>
  178. <input type="checkbox" id="read" name="like" value="0" />
  179. <label for="read">阅读</label>
  180. <input type="checkbox" id="friends" name="like" value="1" />
  181. <label for="friends">交友</label>
  182. <input type="checkbox" id="game" name="like" value="2" />
  183. <label for="game">游戏</label>
  184. </div>
  185. <div>
  186. <label for="phone">手机号:</label>
  187. <input
  188. type="text"
  189. id="phone"
  190. name="phone"
  191. placeholder="请输入11位手机号"
  192. />
  193. </div>
  194. </fieldset>
  195. <fieldset>
  196. <legend>其他</legend>
  197. <div>
  198. <label for="userimg">用户头像:</label>
  199. <input type="file" id="userimg" name="userimg" />
  200. </div>
  201. <div>
  202. <label for="introduce" style="vertical-align: top;"
  203. >个人介绍:</label
  204. >
  205. <textarea
  206. name="introduce"
  207. id="introduce"
  208. cols="30"
  209. rows="3"
  210. placeholder="请输入个人介绍"
  211. ></textarea>
  212. </div>
  213. </fieldset>
  214. </div>
  215. <div class="btnBox">
  216. <button type="submit" class="submitBtn">提交</button>
  217. </div>
  218. </div>
  219. </form>
  220. </div>
  221. </body>
  222. <script>
  223. //给显示密码按钮添加事件
  224. document
  225. .querySelectorAll('input[type="button"]')[0]
  226. .addEventListener("click", showPwd);
  227. document
  228. .querySelectorAll('input[type="button"]')[1]
  229. .addEventListener("click", showPwd);
  230. function showPwd(e) {
  231. let id = e.target.dataset.showid;
  232. let objtype = document.getElementById(id).type;
  233. if (objtype === "text") {
  234. document.getElementById(id).type = "password";
  235. e.target.value = "显示密码";
  236. } else {
  237. document.getElementById(id).type = "text";
  238. e.target.value = "隐藏密码";
  239. }
  240. }
  241. </script>
  242. </html>

预览:

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