CSS: 调用任何具有相同属性的元素

CSS: Calling any element with same attribute

有没有办法调用具有相同属性的网站上的任何元素? 例如,如果我有以下元素:

<h1 example="true">Test</h1>
<p example="true">Test 2</p>

我可以叫他们一起吗?所以不使用以下 -

h1[example="true"]{
    color: red;
}
p[example="true"]{
    color: red;
}

h1[example="true"], p[example="true"]{
    color: red;
}

我只需要一种方法来调用该属性为真的每个元素。

您可以通过指定 CSS [属性] 选择器 直接执行操作,前面没有任何标记名称,如下所示 -

[example="true"]{
  color:red;
}
<h1 example="true">Test</h1>
<p example="true">Test 2</p>

因此,在上面的示例中,example 属性设置为 true 的每个元素都将被 CSS 选择器选中,并且将应用 CSS 样式对他们所有人。

我们可以使用 a[attr=value]h[attr=value] 等属性选择器。考虑上面的代码 - *[attr=value] 选择具有该属性的所有元素。