nqkk58 发表于 2024-6-30 06:31:27

你真的认识 CSS 选取器吗?


    <h2 style="color: black; text-align: left; margin-bottom: 10px;">前言</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">相信<span style="color: black;">针对</span> CSS <span style="color: black;">选取</span>器<span style="color: black;">大众</span>都不陌生吧,但其实<span style="color: black;">选取</span>器的种类还是<span style="color: black;">非常</span>繁多的,直接抱着 W3C 的草案啃<span style="color: black;">亦</span><span style="color: black;">无</span>什么意思。<span style="color: black;">因此</span>本文对 CSS <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>巩固一下<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>器吧。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">元素<span style="color: black;">选取</span>器(Elemental selectors)</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">元素<span style="color: black;">选取</span>器,顾名思义,<span style="color: black;">便是</span><span style="color: black;">经过</span> HTML 元素来进行匹配的<span style="color: black;">选取</span>器。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">类型<span style="color: black;">选取</span>器(Type selectors)</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">类型<span style="color: black;">选取</span>器是<span style="color: black;">经过</span> HTML 元素类型(标签)来<span style="color: black;">选取</span>的:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">// <span style="color: black;">选取</span>所有 &lt;p&gt; 元素
      </span><span style="color: black;">p</span> <span style="color: black;">{</span> <span style="color: black;">font-weigt</span><span style="color: black;">:</span> <span style="color: black;">bold</span><span style="color: black;">;</span> <span style="color: black;">}</span>

      <span style="color: black;">// <span style="color: black;">选取</span>所有 &lt;h1&gt; 元素
      </span><span style="color: black;">h1</span> <span style="color: black;">{</span> <span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;</span> <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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>在每一个<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><span style="color: black;">第1</span>位,否则该<span style="color: black;">选取</span>器就匹配不到任何元素的。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">通用<span style="color: black;">选取</span>器(Universal Selector)</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">通用<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>匹配任意类型的 HTML 元素。<span style="color: black;">因此</span>在单独<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><span style="color: black;">*</span> <span style="color: black;">{</span> <span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;</span> <span style="color: black;">}</span></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<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>器会隐式的存在:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.danger</span> <span style="color: black;">{</span> <span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;</span> <span style="color: black;">}</span>
      <span style="color: black;">// 等同于
      </span><span style="color: black;">*</span><span style="color: black;">.danger</span> <span style="color: black;">{</span> <span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;</span> <span style="color: black;">}</span>
    </div>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">属性<span style="color: black;">选取</span>器(Attribute selectors)</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">属性<span style="color: black;">选取</span>器<span style="color: black;">经过</span>和 HTML 元素的属性进行匹配来<span style="color: black;">选取</span>的,它有以下的写法:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">// <span style="color: black;">持有</span> class 属性的元素
      </span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>

      <span style="color: black;">// <span style="color: black;">持有</span> class 的值为 red 的元素,即 class=red
      </span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">red</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>

      <span style="color: black;">// <span style="color: black;">持有</span> class 的值<span style="color: black;">包括</span> red 的元素,<span style="color: black;">例如</span> class=flex red bold</span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">~=</span><span style="color: black;">red</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>

      <span style="color: black;">// <span style="color: black;">持有</span> class 的值为 red 或以 red- <span style="color: black;">做为</span>前缀的元素,<span style="color: black;">例如</span> class=red-text
      </span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">|=</span><span style="color: black;">red</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>

      <span style="color: black;">// <span style="color: black;">持有</span> class 的值以 red <span style="color: black;">做为</span>前缀的元素,<span style="color: black;">例如</span> class=redtext</span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">^=</span><span style="color: black;">red</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>

      <span style="color: black;">// <span style="color: black;">持有</span> class 的值以 red <span style="color: black;">做为</span>后缀的元素,<span style="color: black;">例如</span> class=textred
      </span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">$</span><span style="color: black;">=</span><span style="color: black;">red</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>

      <span style="color: black;">// <span style="color: black;">持有</span> class 的值<span style="color: black;">包括</span> red 的元素,<span style="color: black;">例如</span> class=teredxt
      </span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">*=</span><span style="color: black;">red</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>

      <span style="color: black;">// 属性<span style="color: black;">选取</span>器<span style="color: black;">能够</span>连续调用
      </span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">~=</span><span style="color: black;">red</span><span style="color: black;">][</span><span style="color: black;">id</span><span style="color: black;">=</span><span style="color: black;">first</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">虽然例子中<span style="color: black;">咱们</span>只<span style="color: black;">运用</span>了 class 属性,<span style="color: black;">不外</span>它<span style="color: black;">能够</span>对任何属性进行匹配,<span style="color: black;">例如</span> id,value 等,<span style="color: black;">或</span><span style="color: black;">咱们</span>自定义的属性。 </p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">体积</span>写区分</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在默认<span style="color: black;">状况</span>下,<span style="color: black;">选取</span>器的<span style="color: black;">体积</span>写区分是和文档的语言相同的,<span style="color: black;">例如</span> HTML 就不区分<span style="color: black;">体积</span>写。<span style="color: black;">不外</span>在 Selectors Level 4 草案中引入了新的标识符 i 和 s 用于手动标识<span style="color: black;">是不是</span><span style="color: black;">必须</span>区分<span style="color: black;">体积</span>写:</p>i 为 Case-insensitive modifier,即不区分<span style="color: black;">体积</span>写s 为 Case-sensitive modifier,即区分<span style="color: black;">体积</span>写<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">// 不区分<span style="color: black;">体积</span>写,即 red, RED, ReD 等都<span style="color: black;">能够</span>匹配</span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">red</span> <span style="color: black;">i</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>

      <span style="color: black;">// 区分<span style="color: black;">体积</span>写,red 匹配,RED 则不匹配
      </span><span style="color: black;">[</span><span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">red</span> <span style="color: black;">s</span><span style="color: black;">]</span> <span style="color: black;">{</span><span style="color: black;">color</span><span style="color: black;">:</span> <span style="color: black;">red</span><span style="color: black;">;}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不外</span><span style="color: black;">必须</span><span style="color: black;">重视</span>的是,标识符 i <span style="color: black;">已然</span>得到主流浏览器的支持,<span style="color: black;">然则</span>标识符 s的支持度则不太<span style="color: black;">阳光</span>:</p>




nykek5i 发表于 3 天前

网站建设seio论坛http://www.fok120.com/
页: [1]
查看完整版本: 你真的认识 CSS 选取器吗?