CSS可以找到具有任何id的元素吗? [英] Can CSS find an element that has ANY id?

查看:132
本文介绍了CSS可以找到具有任何id的元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有许多相同的元素,这些元素不在我的直接控制之下(所以我不能更改HTML)。这可能看起来像这样:

I have a lot of the same elements on a page that is not under my direct control (so i can't change the HTML). This might look like this:

<div class="item">This text should be black</div>
<div class="item" id="brand_one">This text should be red</div>
<div class="item" id="brand_two">This text should be red</div>
...
<div class="item">This text should be black</div>

我想编写一个css规则,将所有

I want to write a css rule that targets all elements with class item that have an id.

我可以做

#brand_one, #brand_two, ... { color:red; }

但是id已经进入了数百个,所以这不是一个选项。

But the id's go into the hundreds, so that's not an option.

我在寻找的是一个这样的规则:

What i'm looking for is a rule something like this:

.item[id] { color:red; } / .item# { color:red; }

我知道这在Javascript中是可行的,但是它存在于CSS中吗?

I know this is possible in Javascript, but does this exist in CSS?

推荐答案

是的,这可以使用 CSS属性选择器

Yes, this is possible using CSS attribute selectors:

.item[id] {
    /* any elements with a class .item and an ID attribute */
}

这篇关于CSS可以找到具有任何id的元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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