CSS 基于元素文本的CSS选择器?

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

CSS selector based on element text?

csscss-selectors

提问by Harry

Is there a way to select an element in css based on element text?

有没有办法根据元素文本在css中选择一个元素?

ie:

IE:

li[text=*foo]

<li>foo</li>
<li>bar</li>

That probably doesn't work.

那可能行不通。

Edit: Also only need to support Chrome.

编辑:也只需要支持 Chrome。

采纳答案by zellio

Not with CSS directly, you could set CSS properties via JavaScript based on the internal contents but in the end you would still need to be operating in the definitions of CSS.

不是直接使用CSS,你可以通过JavaScript根据内部内容设置CSS属性,但最终你仍然需要在CSS的定义中进行操作。

回答by B?a?ej Klisz

I know it's not exactly what you are looking for, but maybe it'll help you.

我知道这不完全是你要找的,但也许它会帮助你。

You can try use a jQuery selector :contains(), add a class and then do a normal style for a class.

您可以尝试使用 jQuery 选择器:contains(),添加一个类,然后为一个类做一个普通的样式。

回答by Agos

It was probably discussed, but as of CSS3 there is nothing like what you need (see also "Is there a CSS selector for elements containing certain text?"). You will have to use additional markup, like this:

它可能被讨论过,但从 CSS3 开始,没有什么比你需要的了(另请参阅“是否有包含特定文本的元素的 CSS 选择器?”)。您将不得不使用其他标记,如下所示:

<li><span class="foo">some text</span></li>
<li>some other text</li>

Then refer to it the usual way:

然后以通常的方式引用它:

li > span.foo {...}