博客列表 >PHP大牛成长之路:完整表格与表单

PHP大牛成长之路:完整表格与表单

周Sir-BLOG
周Sir-BLOG原创
2020年06月17日 19:49:57718浏览

1、表格

1.1、完整表格标签如下表

ID 标签 说明
1 table 定义 HTML 表格。
2 caption 定义表格标题。
3 colgroup 对表格中的列进行组合,以便对其进行格式化。
4 col 为表格中一个或多个列定义属性值。
5 thead 定义表格头部。
6 tbody 定义表格主体。
7 tfoot 定义表格底部。
8 th 定义表格内的表头单元格。
9 tr 定义表格中的行。
10 td 定义表格中的单元格。

1.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>
  8. body{
  9. font: 14px/1.5 Microsoft Yahei,sans-serif;
  10. background-color: #f5f5f5;
  11. color: #424242;
  12. margin: 0;
  13. padding: 0;
  14. }
  15. table{
  16. border-collapse: collapse;
  17. width: 960px;
  18. margin: auto;
  19. text-align: left;
  20. }
  21. table caption{
  22. margin-bottom: 10px;
  23. height: 50px;
  24. font-size: 30px;
  25. font-weight: 400;
  26. color: #757575;
  27. border-bottom: 1px solid #e0e0e0;
  28. background-color: #fff;
  29. }
  30. table thead th{
  31. background-color: #fff;
  32. height: 50px;
  33. font-weight:inherit;
  34. color: #424242;
  35. font-size: 14px;
  36. padding: 5px;
  37. border-bottom: 1px solid #e0e0e0;
  38. }
  39. table tbody tr{
  40. height: 50px;
  41. background-color: #fff;
  42. border-bottom: 1px solid #e0e0e0;
  43. padding: 10px;
  44. }
  45. table tbody tr:hover {
  46. background-color: #f5f5f5;
  47. }
  48. table tbody tr td a{
  49. text-decoration:none;
  50. color: #424242;
  51. }
  52. table tbody tr td a:hover{
  53. text-decoration:none;
  54. color: #ff6700;
  55. }
  56. table tfoot tr td{
  57. background-color: #fff;
  58. height: 50px;
  59. line-height: 50px;
  60. color:#ff6700;
  61. padding: 5px;
  62. }
  63. table tfoot span{
  64. width: 100px;
  65. height: 35px;
  66. line-height: 35px;
  67. display: block;
  68. color: #666;
  69. background-color: #f5f5f5;
  70. text-align: center;
  71. margin: auto 5px;
  72. float: right;
  73. font-weight:bold;
  74. cursor: pointer;
  75. }
  76. table tfoot span:hover{
  77. background-color: #e0e0e0;
  78. color:#ff6700;
  79. }
  80. table tfoot em{
  81. font-size: 1.5rem;
  82. }
  83. </style>
  84. <body>
  85. <!-- 表格 -->
  86. <table>
  87. <!-- 标题 -->
  88. <caption>
  89. 购物车
  90. </caption>
  91. <!-- 头部 -->
  92. <thead>
  93. <tr>
  94. <th>商品名称</th>
  95. <th>单价/元</th>
  96. <th>优惠</th>
  97. <th>数量</th>
  98. <th>金额合计</th>
  99. <th>操作</th>
  100. </tr>
  101. </thead>
  102. <!-- 主体 -->
  103. <tbody>
  104. <tr>
  105. <td>华为p30 pro</td>
  106. <td>3999.00</td>
  107. <td>-500.00</td>
  108. <td>1</td>
  109. <td>3499.00</td>
  110. <td><a href="#">收藏</a> | <a href="#">删除</a></td>
  111. </tr>
  112. <tr>
  113. <td>华为p30 pro碎屏保险</td>
  114. <td>9.90</td>
  115. <td>0</td>
  116. <td>1</td>
  117. <td>9.90</td>
  118. <td><a href="#">收藏</a> | <a href="#">删除</a></td>
  119. </tr>
  120. </tbody>
  121. <!-- 底部 -->
  122. <tfoot>
  123. <tr>
  124. <td colspan="4">合计(不含运费):<em>5555.00</em></td>
  125. <td colspan="2"><span>清空购物车</span><span>去结算</span></td>
  126. </tr>
  127. </tfoot>
  128. </table>
  129. </body>
  130. </html>

2、表单

2.1、基本表单元素

ID 标签 说明
1 form 创建 HTML 表单。
2 fieldset 表单内的相关元素分组。
3 legend 为 fieldset 元素定义标题(caption)
4 label 为 input 元素定义标注(标记)
5 input 用于搜集用户信息,类型:button/checkbox/file/hidden/image/password/radio/reset/submit/text
6 textarea 多行的文本输入控件
7 button 定义一个按钮
8 select 创建单选或多选菜单。
9 datalist 定义选项列表。
10 option 义下拉列表中的一个选项(一个条目)。

除以上基本表单外,还有很多表单元素,后期慢慢学习。

2.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. </head>
  8. <body>
  9. <div>用户注册</div>
  10. <div>
  11. <form action="" method="POST">
  12. <fieldset>
  13. <legend>基本信息(必填)</legend>
  14. <div>
  15. <label
  16. >用户名:
  17. <input
  18. type="text"
  19. name="user"
  20. placeholder="请输入用户名或手机"
  21. autofocus
  22. />
  23. </label>
  24. </div>
  25. <div>
  26. <label
  27. >邮箱:
  28. <input type="email" name="email" placeholder="请输入邮箱" />
  29. </label>
  30. </div>
  31. <div>
  32. <label
  33. >密码: <input type="password"" name="pwd" placeholder="请输入密码"
  34. />
  35. </label>
  36. </div>
  37. <div>
  38. <label
  39. >确认密码:
  40. <input type="password" name="pwd2" placeholder="请再次输入密码" />
  41. </label>
  42. </div>
  43. </fieldset>
  44. <fieldset>
  45. <legend>扩展信息(选填)</legend>
  46. <div>
  47. <label
  48. >生日:
  49. <input type="date" name="birthday" />
  50. </label>
  51. </div>
  52. <div>
  53. <!-- <label for="male"> for对应input ID,点击label可以使input获得焦点 -->
  54. <label for="secret">性别:</label>
  55. <input type="radio" name="gender" value="male" id="male" /><label
  56. for="male"
  57. ></label
  58. >
  59. <input
  60. type="radio"
  61. name="gender"
  62. value="female"
  63. id="female"
  64. /><label for="female"></label>
  65. <input
  66. type="radio"
  67. name="gender"
  68. value="secret"
  69. id="secret"
  70. checked
  71. /><label for="secret">保密</label>
  72. </div>
  73. <div>
  74. <label for="programme">爱好</label>
  75. <!-- name设置数组形式便于后端脚本处理 -->
  76. <input type="checkbox" name="hobby[]" id="php" value="php" /><label
  77. for="php"
  78. >php</label
  79. >
  80. <input
  81. type="checkbox"
  82. name="hobby[]"
  83. value="html"
  84. id="html"
  85. /><label for="html">html</label>
  86. <input
  87. type="checkbox"
  88. name="hobby[]"
  89. value="java"
  90. id="java"
  91. checked
  92. /><label for="java">java</label>
  93. </div>
  94. <div>
  95. <label for="brand">手机品牌:</label>
  96. <input type="search" list="phone" name="brand" id="brand" />
  97. <datalist id="phone">
  98. <option value="apple" label="苹果"></option>
  99. <option value="huawei" label="华为"></option>
  100. <option value="mi" label="小米"> </option>
  101. </datalist>
  102. </div>
  103. </fieldset>
  104. <fieldset>
  105. <legend>其它信息(选填)</legend>
  106. <div>
  107. <label for="uploads">上传头像:</label>
  108. <input
  109. type="file"
  110. name="user_pic"
  111. id="uploads"
  112. accept="image/png, image/jpeg, image/gif"
  113. />
  114. </div>
  115. <div>
  116. <label for="resume">简历:</label>
  117. <textarea
  118. name="resume"
  119. id="resume"
  120. cols="30"
  121. rows="5"
  122. placeholder="不超过100字"
  123. ></textarea>
  124. </div>
  125. </fieldset>
  126. <!-- 隐藏域,用于获取不需要用户输入的数据 -->
  127. <input type="hidden" name="user_id" value="123" />
  128. <p>
  129. <input type="reset" value="重置" class="btn" />
  130. <button class="btn">提交</button>
  131. </p>
  132. </form>
  133. </div>
  134. </body>
  135. </html>

由于时间关系,未作样式优化,仅演示基础用法。

总结

  • 对表格有了一定的理解,通过表格,可以很方便的展现数据列表; 除了表格外,任何标签都可以使用CSS的display属性变为表格形式。
  • 对常用表单元素进行了学习,理解了获取用户信息所用的元素,期待后期的课程,如何获取表单的值。
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议