CSS selector by inline style attribute(内联样式属性的 CSS 选择器)
问题描述
是否有 CSS 选择器可以通过其内联样式属性值选择此元素?
Is there a CSS selector to select this element by its inline style attribute value?
<div style='display:block'>...</div>
类似
div[cssAttribute=cssValue]
推荐答案
内联 style
属性与任何其他 HTML 属性没有区别,可以与子字符串属性选择器匹配:
The inline style
attribute is no different to any other HTML attribute and can be matched with a substring attribute selector:
div[style*="display:block"]
正是由于这个原因,它非常脆弱.由于属性选择器不支持正则表达式,您只能对属性值执行 exact 子字符串匹配.例如,如果您在属性值的某处有一个空格,如下所示:
It is for this very reason however that it's extremely fragile. As attribute selectors don't support regular expressions, you can only perform exact substring matches of the attribute value. For instance, if you have a space somewhere in the attribute value, like this:
<div style='display: block'>...</div>
在您更改选择器以适应空间之前,它不会匹配.然后它将停止匹配 不 包含空格的值,除非你包含所有排列,令人作呕.但是,如果您正在处理的文档中的内联样式声明本身不太可能发生变化,那么您应该没问题.
It won't match until you change your selector to accommodate the space. And then it will stop matching values that don't contain the space, unless you include all the permutations, ad nauseam. But if you're working with a document in which the inline style declarations themselves are unlikely to change at all, you should be fine.
还请注意,这不是根据它们在 DOM 中反映的实际指定、计算或使用的值来选择元素.这在 CSS 选择器中是不可能的.
Note also that this is not at all selecting elements by their actual specified, computed or used values as reflected in the DOM. That is not possible with CSS selectors.
这篇关于内联样式属性的 CSS 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:内联样式属性的 CSS 选择器


- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01