CSS 如何选择ID以特定字符串开头和结尾的所有元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16009755/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:27:22  来源:igfitidea点击:

How to select all elements whose ID starts and ends with specific strings?

csscss-selectors

提问by Danny Beckett

In CSS, how can I select all <tr>elements where their idbegins with section_and ends with _dummy?

在 CSS 中,如何选择<tr>id开头section_和结尾的所有元素_dummy

E.g. I'd like to select and apply styles the following <tr>s:

例如,我想选择并应用以下样式<tr>

<tr id="section_5_1_dummy">
    <td>...</td>
</tr>

<tr id="section_5_2_dummy">
    <td>...</td>
</tr>

回答by Danny Beckett

The following CSS3 selector will do the job:

以下 CSS3 选择器将完成这项工作:

tr[id^="section_"][id$="_dummy"] {
    height: 200px;
}

The ^denotes what the idshould begin with.

^表示什么id应该开始。

The $denotes what the idshould end with.

$表示什么id应该结束。



iditself can be replaced with another attribute, such as href, when applied to (for example) <a>:

id本身可以替换为另一个属性,例如href,当应用于(例如)时<a>

a[href^="http://www.example.com/product_"][href$="/about"] {
    background-color: red;
}