CSS选择器的样式属性 [英] CSS selector by style attribute

查看:164
本文介绍了CSS选择器的样式属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个CSS选择器(意味着不是jQuery)通过样式表上的样式属性值选择此元素?

there is a CSS Selector ( it means not jQuery ) to select this element by his style attribute value on a style sheet?

<div style='display:block'>...</div>

例如

div[cssAttribute=cssValue]


推荐答案

不可能使用CSS通过他们的CSS规则(声明的样式,计算的样式,所有其他复杂的东西...)选择元素。

It's impossible to use CSS to select elements by their CSS rules (declared styles, computed styles, all the other complicated things...).

使用子串属性选择器检查内置 style 属性...

The closest you can get is to check the inline style attribute with a substring attribute selector...

div[style*="display:block"]

脆弱。由于CSS不支持属性选择器中的正则表达式,因此您只能选择属性值的 exact 匹配项。例如,如果您在某处有空格,像这样:

As you can expect it's extremely fragile. As CSS doesn't support regular expressions in attribute selectors, you can only select exact matches of the attribute value. For instance, if you have a space somewhere, like this:

<div style='display: block'>...</div>

除非您更改选择器以包含空格,否则不匹配。

It won't match unless you change your selector to contain the space.

但是,如果你通过内联样式属性来查询HTML DOM中的元素来进行测试或调试(比如Selenium),这将会做到。您通常无法更改显示给您的标记,也不会对应用样式感兴趣。

However, if you're querying elements in an HTML DOM by inline style attributes for testing or debugging purposes (think Selenium), this will do. You usually don't have the ability to change the markup as presented to you, nor are you usually interested in applying styles.

这篇关于CSS选择器的样式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆