CSS 选择器(id 包含部分文本)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12155833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS selector (id contains part of text)
提问by TarasLviv
I have a question. I have elements something like this:
我有个问题。我有这样的元素:
<a>
element with id = someGenerated Some:Same:0:name
<a>
id = someGenerated Some:Same:0:name 的元素
<a>
element with id = someGenerated Some:Same:0:surname
<a>
id = someGenerated Some:Same:0:surname 的元素
<a>
element with id = someGenerated Some:Same:1:name
<a>
id = someGenerated Some:Same:1:name 的元素
<a>
element with id = someGenerated Some:Same:1:surname
<a>
id = someGenerated Some:Same:1:surname 的元素
I need CSS selector to get names. The problem is that I don't know how to get it.
I tried a[id*='Some:Same']
- it returned all <a>
elements. After I can get elements which id ends with name. But I don't like this idea. I think that it can be done with some other selector.
我需要 CSS 选择器来获取名称。问题是我不知道如何得到它。我试过a[id*='Some:Same']
- 它返回了所有<a>
元素。在我可以获得 id 以 name 结尾的元素之后。但我不喜欢这个主意。我认为它可以用其他一些选择器来完成。
回答by CosminO
Try this:
尝试这个:
a[id*='Some:Same'][id$='name']
This will get you all a
elements with id containing
这将为您a
提供 id 包含的所有元素
Some:Same
一些相同
and have the id ending in
并且以 id 结尾
name
姓名
回答by Edicarlos Lopes
<div id='element_123_wrapper_text'>My sample DIV</div>
The Operator ^- Match elements that starts with given value
运算符^- 匹配以给定值开头的元素
div[id^="element_123"] {
}
The Operator $ - Match elements that ends with given value
运算符 $ - 匹配以给定值结尾的元素
div[id$="wrapper_text"] {
}
The Operator * - Match elements that have an attribute containing a given value
运算符 * - 匹配具有包含给定值的属性的元素
div[id*="wrapper_text"] {
}
回答by LeBen
The only selector I see is a[id$="name"]
(all links with id finishing by "name") but it's not as restrictive as it should.
我看到的唯一选择器是a[id$="name"]
(所有带有 id 的链接以“名称”结尾),但它没有应有的限制。