博客列表 >第四课:选择器与表格

第四课:选择器与表格

XFY_肆意De...
XFY_肆意De...原创
2020年06月17日 16:18:56807浏览

第四课:选择器与表格

选择器:简单选择器[标签选择器]
  • 描述:浏览器能直接显示出来的选择器及为简单选择器
  • 元素选择器、类选择器、ID选择器、伪类
  • 注意:复合类样式[层叠]的style中.class 之间不能有空格,且id,class可以添加到任何元素上使用!
  • 样式累加 :相同元素,后面追加的样式会覆盖前面的样式,但是,这个覆盖样式要遵循样式优先级的问题;
  • 元素选择器 < 类选择器 < id选择器 < 内联样式
  • 区别:#first > 单独的id选择器 #first.itme > #firs 级别
    ID选择器 应用场景 只有二种场景:表单元素,锚点。

  1. <style>
  2. .item{
  3. ......
  4. }
  5. .item.center{
  6. .....
  7. }
  8. /*id 选择器 [以“#”代表]*/
  9. #nav_id{
  10. ....
  11. }
  12. </style>
  13. <div class="item center" id="nav_id{
  14. ....
  15. }"></div>

上下文选择器

  • 后代选择器 [中间有空格]
  1. <style>
  2. .container div{
  3. ......
  4. }
  5. </style>
  6. <div class="container">
  7. <div class="item">1</div>
  8. <div class="item">2</div>
  9. <div class="item">3</div>
  10. </div>
  • 父子选择器 [用“>”来标示]
  1. <style>
  2. .container > div{
  3. ......
  4. }
  5. </style>
  6. <div class="container">
  7. <div class="item">1</div>
  8. <div class="item">2</div>
  9. <div class="item">3</div>
  10. </div>
  • 选择器
    在已知自己需要定位到某个目标上的时候可以使用类标签名的方式去定位。
  1. <style>
  2. /*同级相邻选择器*/
  3. .item.center + .item{
  4. ......
  5. }
  6. /*同级所有选择器*/
  7. .item.center ~ .item{
  8. ......
  9. }
  10. </style>
  11. <div class="container">
  12. <div class="item">1</div>
  13. <div class="item center">2</div>
  14. <div class="item">3</div>
  15. <div class="item">4</div>
  16. <div class="item">5</div>
  17. </div>

伪类选择器 [不分组](不区分元素类型)省略父元素。

  1. /*
  2. 语法::first-child{}
  3. 解释:为第个子元素添加属性;
  4. 没有指定具体元素的话,默认就是所有的“*” 依此查询所有的第一个子元素添加
  5. 生效范围是整个HTML文档,所有的父亲
  6. PS:必须指定一个父元素,尽量用父子关系指定;
  7. .container :first-child{
  8. ....
  9. }
  10. */
  11. <style>
  12. /*给container下的第一元素添加属性*/
  13. .container > :first-child{
  14. ....
  15. }
  16. /*给container下的最后一个元素添加属性*/
  17. .container > :last-child{
  18. ....
  19. }
  20. /*选择第三个,下标从1开始*/
  21. .container > :nth-chlid(3){
  22. .....
  23. }
  24. /*选择偶数列 even 代替 2n*/
  25. .container > :nth-chlid(2n){
  26. ....
  27. }
  28. /*选择奇数列 odd 代替 2n-1 */
  29. .container > :nth-chlid(2n-1){
  30. ....
  31. }
  32. /*从指定位置开始,选择剩下的所有元素*/
  33. .container .item:nth-chlid(n + 3){
  34. ....
  35. }
  36. /*只取前三个*/
  37. .container .item:nth-chlid(-n + 3){
  38. ....
  39. }
  40. /*只取最后三个 */
  41. .container .item:nth-last-chlid(-n + 3){
  42. ....
  43. }
  44. /*只取最后三个 取后面的用倒数的方式去取*/
  45. .container .item:nth-last-chlid(2){
  46. ....
  47. }
  48. </style>
  49. <div class="container">
  50. <div class="item">1</div>
  51. <div class="item center">2</div>
  52. <div class="item">3</div>
  53. <div class="item">4</div>
  54. <div class="item">5</div>
  55. </div>

伪类选择器 [分组]

  • 分组结构伪类;
    1.对元素按类型进行分组;
    2.在分组中根据索引进行选择;
  1. <style>
  2. /*不限定的话就对container下的分组每个组里面的最后一个如果要指定,则使用元素的例如:
  3. .container span:last-of-type{}
  4. */
  5. .container :last-of-type{
  6. ...
  7. }
  8. /*匹配任何一个*/
  9. .container span:nth-of-type(3){
  10. ......
  11. }
  12. /*前三个*/
  13. .container span:nth-of-type(-n + 3){
  14. ......
  15. }
  16. /*最后二个*/
  17. .container span:nth-last-of-type(-n + 2){
  18. ......
  19. }
  20. </style>
  21. <div class="container">
  22. <div class="item">1</div>
  23. <div class="item center">2</div>
  24. <span class="item">3</span>
  25. <span class="item">4</span>
  26. <span class="item">5</span>
  27. </div>
  28. `````
  29. ####伪类/伪元素
  30. - :target 必须和ID配合,实现锚点操作!
  31. - :focus 当获取焦点的时候
  32. - ::selection 只允许设置选中文本的前景色和背景色
  33. - :not 不满足条件的
  34. - ::before 在什么之前生成某些东西
  35. - ::atfer 在什么之后生成某些东西
  36. - 伪类是单冒号,伪元素是双冒号。
  37. ````
  38. <style>
  39. #login-from{
  40. display:none;
  41. }
  42. #login-from:target{
  43. display:block;
  44. }
  45. inputfocus{
  46. ....
  47. }
  48. input::selection{
  49. color:...;
  50. backguround-color:...;
  51. }
  52. </style>
  53. <body>
  54. <button onclick="location='#login-from'"></botton>
  55. <form id="login-from">
  56. <label for="email">邮件</label>
  57. <input type="text" name="email" id="email">
  58. <label for="password">邮件</label>
  59. <input type="text" name="password" id="password">
  60. </form>
  61. <hr/>
  62. ------------------------------------------
  63. #### :not()排除不需要的元素
  64. <style>
  65. .list > :not(:nth-of-type(3)){
  66. color:red;
  67. .list::before{
  68. content:‘购物车’;
  69. color:red;
  70. }
  71. .list::after{
  72. content:‘清算中’;
  73. color:red;
  74. }
  75. }
  76. </style>
  77. <ul class="list">
  78. <li>1</li>
  79. <li>2</li>
  80. <li>3</li>
  81. <li>4</li>
  82. </ul>
  83. </body>
  84. `

表格的组成部分
说明 语法结构
表格 table
标题 caption
分组 colgroup
表头 thead
主体 <tbody>
底部 tfoot
表格标题 th
tr
td
  1. <table>
  2. <cation>
  3. 员工信息表
  4. </cation>
  5. <colgroup>
  6. <col></col>
  7. <col></col>
  8. <col></col>
  9. </colgroup>
  10. <thead>
  11. <tr>
  12. <th>ID</th>
  13. <th>name</th>
  14. <th>passs</th>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. <tr>
  19. <td>1</td>
  20. <td>jack</td>
  21. <td>123456</td>
  22. </tr>
  23. </tbody>
  24. <tfoot>
  25. <tr>
  26. <td>1</td>
  27. <td>1</td>
  28. <td>1</td>
  29. </tr>
  30. </tfoot>
  31. </table>
  32. `
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议