非空属性的CSS属性选择器


90

是否有适用于非空属性的CSS选择器?:not([Data-Attribute=''])如果属性也不存在,则使用匹配项。

我正在寻找类似的东西[Data-Attribute!='']


[Data-Attribute!='']如果您谈论的是jQuery选择器,则不会相似,因为:not([Data-Attribute=''])如果未指定该属性,则jQuery选择器等效于并且确实匹配。
BoltClock

Answers:


191

试试这个

<style>
    [Data-Attribute]:not([Data-Attribute=""])
    {
        background-color: Red;
    }
</style>

8
我在这里看到了一个更好(或不同)的解决方案:stackoverflow.com/questions/16429220/…–
Sven

6
@Sven该链接上唯一可以回答此问题(选择不为空的特定数据属性)的解决方案与此答案相同。
rybo111

是的,这里提供的解决方案针对他的需求,上面链接中的一种要求采用反样式。

此解决方案会影响意外元素(如果匹配attr title)。
帕维尔·弗拉索夫'18
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.