且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取CSS选择以字符串(不是Javascript)开头的ID?

更新时间:2023-11-26 19:34:52

  [id ^ = product] 


$ b b

^ = 表示开头为。相反, $ = 表示结束于。



符号实际上是从Regex语法借用的,其中 ^ $ 分别表示字符串开始和字符串结束。



请参阅规格了解完整信息。


If the HTML has elements like this:

id="product42"
id="product43"
...

How do I match all of those id's starting with "product"?

I've seen answers that do this exactly using javascript, but how to do it with only CSS?

[id^=product]

^= indicates "starts with". Conversely, $= indicates "ends with".

The symbols are actually borrowed from Regex syntax, where ^ and $ mean "start of string" and "end of string" respectively.

See the specs for full information.