CSS3新增屬性選取器: [attribute*=value] 、[attribute^=value] 和[attribute$=value],css3attribute

來源:互聯網
上載者:User

CSS3新增屬性選取器: [attribute*=value] 、[attribute^=value] 和[attribute$=value],css3attribute

在CSS3中新增了 [attribute*=value] 、[attribute^=value] 和[attribute$=value] 三個選取器,使得屬性選取器有了萬用字元的概念。

下邊是一個使用這三個屬性的完整範例程式碼,及運行後的頁面展示效果:

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <style>        [id *= irs] {            background-color: red;        }        [id ^= sec] {            background-color: blue;        }        [id $= ird] {            background-color: green;        }    </style></head><body>    <div id="first">第一個 div 元素。(*= 部分匹配)</div>    <div id="second">第二個 div 元素。(^= 開頭匹配)</div>    <div id="third">第三個 div 元素。($= 結尾匹配)</div></body></html>

瀏覽器中運行效果展示:

下邊以這個例子說明CSS3屬性選取器。

1. [attribute*=value] 屬性選取器

[attribute *= value] 屬性選取器含義是:選擇attribute屬性中含有value字串的HTML元素。

上邊例子中[id *= irs],attribute就是id,value就是irs,這樣就是要選擇id屬性中含有“irs”字串的元素,即第一個div元素,並將這個元素的背景色設定為紅色。

2. [attribute^=value] 屬性選取器

[attribute ^= value] 屬性選取器含義是:選擇attribute屬性中以value字串開頭的HTML元素。

上邊例子中[id ^= sec],attribute就是id,value就是sec,這樣就是要選擇id屬性中以“sec”字串開頭的元素,即第二個div元素,並將這個元素的背景色設定為藍色。

3. [attribute$=value] 屬性選取器

[attribute $= value] 屬性選取器含義是:選擇attribute屬性中以value字串結尾的HTML元素。

上邊例子中[id $= ird],attribute就是id,value就是ird,這樣就是要選擇id屬性中以“ird”字串結尾的元素,即第三個div元素,並將這個元素的背景色設定為綠色。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.