html - CSS 属性选择器以无属性名称开头

标签 html css

我知道我们可以在给定属性名称的情况下选择以“foo”开头的属性。

div[class^="foo"]

可以在没有属性名称的情况下完成此操作吗?

div[^="foo"]

注意:

我知道我们可以做到这一点:

div[foo] {
  color: blue;
}

但就我而言,我尝试选择具有可变属性名称的内容:

foo-1 foo-2 等等

div[^=foo] {
  color: red;
}

div[class^="foo"] {
  color: red;
}
<div foo>This text is not red</div>

<div class="foo">This text is red</div>

最佳答案

Can this be done without an attribute name?

虽然答案不太漂亮,但缺点是,但它不能 - 不是按照您建议的方式。


引用文献:

W3 Attribute Selectors Specification

[att] Represents an element with the att attribute, whatever the value of the attribute.

[att=val] Represents an element with the att attribute whose value is exactly "val".

[att~=val] Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If "val" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "val" is the empty string, it will never represent anything.

[att|=val] Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-" (U+002D). This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML) as described in BCP 47 ([BCP47]) or its successor. For lang (or xml:lang) language subcode matching, please see the :lang pseudo-class.


W3 General Selectors Specification

The following table summarizes the Selector syntax:

...

[Provides documentation of all valid selectors, omitting any mention of wildcard attribute selectors]

...

关于html - CSS 属性选择器以无属性名称开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43614316/

相关文章:

html - 带有 CSS 的倾斜图像框

mysql - 如何从数据库中检索特殊字符?

html - 一般来说,HTML5 大纲算法对 SEO 有多大提升?

html - 与 Icon::before 的链接,在多行时应该很好地中断

javascript - 达到最大长度限制时更改输入字段样式

javascript - SVG <path href =""> 问题

html - &lt;!-- [if IE]> 应用边距不应用样式

javascript - 添加颜色后,简单的基于 jQuery 的 ascii 动画崩溃

HTML 电子邮件 : Images go beyond its limited width in outlook

css - 需要 Jstree css,它使用黑色大箭头来打开和关闭节点,类似于下图