問題描述
我閱讀了關于在 CSS 選擇器中使用正則表達式的本教程并試圖推斷:是否有 CSS 速記來執行以下操作?我想選擇具有a"、b"、c"或d"附加類的foo"類的所有 div.
I read this tutorial on using regular expressions with CSS selectors and am trying to extrapolate: Is there a CSS shorthand to do the following? I want to select all div's with class of "foo" that have either an additional class of "a", "b", "c", or "d".
.foo.a,
.foo.b,
.foo.c,
.foo.d {
/* stuff */
}
類似:
.foo[class~='a','b','c','d'] {}
?
推薦答案
目前不可能(使用 Selectors 3 建議).CSS 沒有成熟的正則表達式語法,也沒有辦法分解多個選擇器列表的一部分.
It's not possible, currently (with the Selectors 3 recommendation). There isn't a full-fledged regex grammar for CSS, nor is there a way to crunch down parts of multiple selector lists.
Selectors 4 提出了一個新的 :matches()
基于 Mozilla 原始 :any()
實現的偽類(參見 this answer 了解一些歷史):
Selectors 4 proposes a new :matches()
pseudo-class based on Mozilla's original :any()
implementation (see this answer for a bit of history):
.foo:matches(.a, .b, .c, .d)
當然,不要指望瀏覽器支持這一點.我什至可能會忘記在那個時候更新這個答案,但是......我們會看到的.
Of course, don't expect browser support for this yet. I might even forget to update this answer when that time comes but... we'll see.
這篇關于用于識別多個類的 CSS 速記的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!