tw4ld6 发表于 2024-6-30 11:05:34

CSS选取器


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.<span style="color: black;">选取</span>器的功能</p>将所<span style="color: black;">必须</span>的元素选中,<span style="color: black;">能够</span>操作这些元素的CSS样式<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.基本<span style="color: black;">选取</span>器<span style="color: black;">归类</span>(常用<span style="color: black;">选取</span>器)</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">// 语法格式
      <span style="color: black;">选取</span>器{
      属性1 : 值1;
      属性2 : 值2;
      ...
      }
    </div>元素<span style="color: black;">选取</span>器以元素的名字<span style="color: black;">做为</span><span style="color: black;">选取</span>器id<span style="color: black;">选取</span>器以id名字<span style="color: black;">做为</span><span style="color: black;">选取</span>器区别class: id是身份证,<span style="color: black;">拥有</span><span style="color: black;">独一</span>性,<span style="color: black;">因此</span><span style="color: black;">不可</span><span style="color: black;">同期</span>设置多个相同的id名id名字:以“#”号开头class<span style="color: black;">选取</span>器以class名字<span style="color: black;">做为</span><span style="color: black;">选取</span>器<span style="color: black;">能够</span>存在多个相同的class名字class名字:以“.”号开头后代<span style="color: black;">选取</span>器(多个选项用空格隔开)<span style="color: black;">便是</span><span style="color: black;">选取</span>元素内部某一种元素的所有元素:<span style="color: black;">包含</span>子元素和其他后代元素<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"father"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">span</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"son1"</span><span style="color: black;">&gt;&lt;/</span><span style="color: black;">span</span><span style="color: black;">&gt;</span> //选中它 -----&gt; .father span{} <span style="color: black;">或</span> .father .son1{}
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"son2"</span><span style="color: black;">&gt;&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>// 选中它 -----&gt; .father div{} <span style="color: black;">或</span> .father .son2{}<span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
    </div>群组<span style="color: black;">选取</span>器(多个选项用逗号隔开)指<span style="color: black;">同期</span>对几个<span style="color: black;">选取</span>器进行相同的操作<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">span</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"son1"</span><span style="color: black;">&gt;&lt;/</span><span style="color: black;">span</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"son2"</span><span style="color: black;">&gt;&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>// 选中body里面的两个标签 ----&gt; span,div{} <span style="color: black;">或</span> .son1,.son2{}等方式
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3.层次<span style="color: black;">选取</span>器<span style="color: black;">归类</span>(进阶<span style="color: black;">选取</span>器)</p>后代<span style="color: black;">选取</span>器( M N )<span style="color: black;">选取</span>M元素内部后代的N元素(所有N元素,<span style="color: black;">包含</span>子代元素或孙代元素)<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (1)
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (2)
      <span style="color: black;">&lt;</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>孙元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (3)
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>孙元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (4)
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (5)
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>

      div p{} -----&gt; 会选中 (1)(2)(3)(4)(5)
    </div>子代<span style="color: black;">选取</span>器( M &gt; N )<span style="color: black;">选取</span>M元素内部子代的N元素(所有第 1 级N元素)<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (1) // <span style="color: black;">第1</span>级 p元素
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (2) // 第二季 p元素
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>

      div &gt; p{} ------&gt; 会选中 (1)
    </div>兄弟<span style="color: black;">选取</span>器( M ~ N)<span style="color: black;">选取</span>M元素后的所有同级N元素(只<span style="color: black;">选取</span>后面的兄弟元素,不<span style="color: black;">选取</span>前面的兄弟元素)<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (1)
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (2)
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">id</span><span style="color: black;">=</span><span style="color: black;">"brother_div"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>孙元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (3)
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>孙元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (4)
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (5)
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (6)
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>

      #brother_div ~ p {} -----&gt; 会选中(5)(6),不会选中(1)(2)
    </div>相邻<span style="color: black;">选取</span>器( M + N)(区别兄弟<span style="color: black;">选取</span>器)<span style="color: black;">选取</span>M元素相邻的下一个N元素(M、N是同级元素)常用场景:用于两个元素之间实现效果<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (1)
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (2)
      <span style="color: black;">&lt;</span><span style="color: black;">div</span> <span style="color: black;">id</span><span style="color: black;">=</span><span style="color: black;">"brother_div"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>孙元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (3)
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>孙元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (4)
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>(5)<span style="color: black;">&lt;</span><span style="color: black;">p</span><span style="color: black;">&gt;</span>子元素<span style="color: black;">&lt;/</span><span style="color: black;">p</span><span style="color: black;">&gt;</span> (6)
      <span style="color: black;">&lt;/</span><span style="color: black;">div</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">body</span><span style="color: black;">&gt;</span>

      #brother_div + p {} ------&gt; 选中 (5)
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4.:first-letter和:first-line<span style="color: black;">选取</span>器</p>:first-letter<span style="color: black;">选取</span>器选中元素内容的“<span style="color: black;">第1</span>个字”<span style="color: black;">或</span>“<span style="color: black;">第1</span>个字母”:first-line<span style="color: black;">选取</span>器(只针对文字,不针对字母)选中元素内容的“<span style="color: black;">第1</span>行文字”


akbchina.cn 发表于 2024-9-9 00:44:03

感谢楼主的分享!我学到了很多。

ejn2tr4200 发表于 2024-9-9 20:15:47

外贸论坛是我们的,责任是我们的,荣誉是我们的,成就是我们的,辉煌是我们的。

wrjc1hod 发表于 2024-10-21 13:26:56

楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/

1fy07h 发表于 2024-10-28 00:10:29

一看到楼主的气势,我就觉得楼主同在社区里灌水。

j8typz 发表于 5 小时前

论坛外链网http://www.fok120.com/
页: [1]
查看完整版本: CSS选取器