Blogger Information
Blog 13
fans 2
comment 0
visits 10229
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
前端表格与表单的实战
北纬38
Original
672 people have browsed it

1.表格实现购物车


知识点:

标签 描述
table 定义表格
th 定义表头
tr 定义行
td 定义列
caption 定义标题
thead 定义表格头部
tbody 定义表格主体
tfoot 定义表格底部
display 设置元素显示类型

表格HTML代码:

  1. <table>
  2. <!-- 标题 -->
  3. <caption>京东618购物车</caption>
  4. <!-- 头部 -->
  5. <thead>
  6. <tr>
  7. <th>序号</th>
  8. <th>名称</th>
  9. <th>单价</th>
  10. <th>数量</th>
  11. <th>总价</th>
  12. </tr>
  13. </thead>
  14. <!-- 主体 -->
  15. <tbody>
  16. <tr>
  17. <td>1</td>
  18. <td>华为P40Pro</td>
  19. <td>5800</td>
  20. <td>1</td>
  21. <td>5800</td>
  22. </tr>
  23. <tr>
  24. <td>2</td>
  25. <td>联想X1笔记本电脑</td>
  26. <td>13000</td>
  27. <td>1</td>
  28. <td>13000</td>
  29. </tr>
  30. <tr>
  31. <td>3</td>
  32. <td>飞利浦理发器</td>
  33. <td>240</td>
  34. <td>1</td>
  35. <td>240</td>
  36. </tr>
  37. <tr>
  38. <td>4</td>
  39. <td>电脑椅</td>
  40. <td>2500</td>
  41. <td>1</td>
  42. <td>2500</td>
  43. </tr>
  44. </tbody>
  45. <!-- 底部 -->
  46. <tfoot>
  47. <td colspan="4">合计</td>
  48. <td>21540</td>
  49. </tfoot>
  50. </table>
  51. <!-- 结算 -->
  52. <div>
  53. <button>结算</button>
  54. </div>

CSS样式代码:

  1. <style>
  2. html{font-size: 20px;}
  3. /* 去掉单元格间隔 */
  4. table{
  5. border-collapse: collapse;
  6. width: 50%;
  7. margin: auto;
  8. color: black;
  9. font-weight: lighter;
  10. text-align: center;
  11. }
  12. /* 表格线直接加单元格上面 */
  13. table thead th,
  14. table td{
  15. border-bottom: 1px solid #000;
  16. padding: 20px;
  17. }
  18. /* 标题样式 */
  19. table caption{
  20. font-size: 1.5rem;
  21. font-family: STHUPO;
  22. color: darkred;
  23. margin-bottom: 18px;
  24. }
  25. /* 表头样式 */
  26. table th{
  27. color: chocolate;
  28. background-color: deepskyblue;
  29. }
  30. /* 隔行变色 */
  31. table tbody tr:nth-of-type(even){
  32. background-color: greenyellow;
  33. }
  34. /* 鼠标悬停效果 */
  35. table tbody tr:hover{
  36. background-color: orangered;
  37. }
  38. /* 底部样式 */
  39. table tfoot td{
  40. border-bottomnone;
  41. color: orangered;
  42. font-size: 20px;
  43. font-weight: bolder;
  44. }
  45. /* 结算按钮 */
  46. body div:last-of-type{
  47. width: 50%;
  48. margin: 15px auto;
  49. }
  50. body div:first-of-type button{
  51. /* 靠右 */
  52. float: right;
  53. width: 120px;
  54. height: 40px;
  55. background-color: darkorange;
  56. color: white;
  57. border: none;
  58. font-size: large;
  59. /* 设置鼠标样式 */
  60. cursor: pointer;
  61. }
  62. body div:first-of-type button:hover{
  63. background-color: violet;
  64. font-size: 1.1rem;
  65. }
  66. </style>

实例图:

2.换个姿势玩表格

HTML代码部分:

  1. <body>
  2. <!-- 表格:<table>< -->
  3. <div class="table">
  4. <!-- 标题 -->
  5. <div class="table-caption">选拔信息表</div>
  6. <!-- 列分组 -->
  7. <div class="table-colgroup">
  8. <div class="table-col"></div>
  9. <div class="table-col"></div>
  10. <div class="table-col"></div>
  11. </div>
  12. <!-- 表头 -->
  13. <div class="table-thead">
  14. <!-- 行 -->
  15. <div class="table-row">
  16. <div class="table-cell">ID</div>
  17. <div class="table-cell">姓名</div>
  18. <div class="table-cell">性别</div>
  19. </div>
  20. </div>
  21. <!-- 主体 -->
  22. <div class="table-tbody">
  23. <div class="table-row">
  24. <div class="table-cell">1</div>
  25. <div class="table-cell">张飞</div>
  26. <div class="table-cell"></div>
  27. </div>
  28. <div class="table-row">
  29. <div class="table-cell">2</div>
  30. <div class="table-cell">李丹</div>
  31. <div class="table-cell"></div>
  32. </div>
  33. <div class="table-row">
  34. <div class="table-cell">3</div>
  35. <div class="table-cell">杜子腾</div>
  36. <div class="table-cell"></div>
  37. </div>
  38. <div class="table-row">
  39. <div class="table-cell">4</div>
  40. <div class="table-cell">熊莉</div>
  41. <div class="table-cell"></div>
  42. </div>
  43. </div>
  44. <!-- 底部<tfoot> -->
  45. <div class="table-tfoot">
  46. <div class="table-row">
  47. <div class="table-cell">X</div>
  48. <div class="table-cell">Y</div>
  49. <div class="table-cell">Z</div>
  50. </div>
  51. </div>
  52. </div>
  53. </body>

CSS代码部分:

  1. <style>
  2. /* 表格 */
  3. .table {
  4. display: table;
  5. border: 1px solid #000;
  6. width: 500px;
  7. text-align: center;
  8. margin: auto;
  9. }
  10. /* 标题 */
  11. .table-caption {
  12. display: table-caption;
  13. margin-bottom: 16px;
  14. font-size: 1.5rem;
  15. }
  16. /* 表头 */
  17. .table-thead {
  18. display: table-header-group;
  19. background-color: darkgrey;
  20. }
  21. /* 行 */
  22. .table-row {
  23. display: table-row;
  24. }
  25. /* 列 */
  26. .table-cell {
  27. display: table-cell;
  28. border: 1px solid #000;
  29. padding: 10px;
  30. }
  31. /* 主体 */
  32. .table-tbody {
  33. display: table-row-group;
  34. }
  35. /* 底部 */
  36. .table-tfoot {
  37. display: table-footer-group;
  38. }
  39. /* 列分组样式 */
  40. .table-colgroup {
  41. display: table-column-group;
  42. }
  43. .table-colgroup .table-col:first-of-type {
  44. display: table-column;
  45. background-color: darkslategray;
  46. }
  47. .table-colgroup .table-col {
  48. display: table-column;
  49. background-color: deepskyblue;
  50. }
  51. </style>

实例图:

3.表单实现用户注册

标签 描述
<fieldset>.. < /fieldset> 控件组
placeholder 提示信息
autofocus 自动获取焦点
email类型 <input type="email">
密码类型 <input type="password" name="password">
<legend> ..</legend> 扩展信息
radio 单选
checkbox 复选/多选
日期选择器 <input type="date">
textarea 文本域
file 文件上传
hidden 隐藏域

HTML代码部分

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

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.总结:

  • 表格: 注意布局格式
  • 表单:注意控件元素搭配
  • 整体上还是比较复杂的,后期可能会用到的比较多,还是要花时间多练习才能熟练使用。
Correcting teacher:WJWJ

Correction status:qualified

Teacher's comments:写的很不错,能看出来下了功夫,继续努力加油!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!