在HTML属性的一部分中设置CSS属性是否可能?

huangapple go评论44阅读模式
英文:

CSS: Is it possible to set a CSS property to a portion of an HTML Attribute?

问题

I have translated the provided text into Chinese:

让我们假设我有一个类似这样的属性:test="key1-Value1 key2-Value2"。我知道有attr()函数用于获取属性值,但如果我只想要属性中的Value2,应该怎么办?如果可以的话,我会使用正则表达式,类似这样:查找key-2(.+)$,并替换为$1。你有任何想法,如果可能的话,我该如何实现这个?

编辑: 我想要能够拥有上述指定的属性,并能够编写类似于color: Value2的属性,其中Value2是从属性中提取的。

我想要拥有许多不同元素,具有不同的属性,而不必为它们硬编码所有规则。理想情况下,我可以只使用CSS来实现这一点,因为我正在为一个Electron应用程序编写主题,而我不能(除非绕道而行)为其编写自定义JS。

英文:

Let's say I have a attribute like this: test="key1-Value1 key2-Value2". I know there's the attr() function for getting attribute values, but what if I wanted just Value2 in the attribute? If I could, I'd use regex like this: find key-2(.+)$, and replace with $1. Do you have any ideas how I could accomplish this, if possible?

Edit: I want to be able to have an attribute like specified above and be able to write properties like for example, color: Value2, with Value2 extracted from the attribute.

I want to have a bunch of different elements with varying attributes, and not have to hardcode all the rules for them. Ideally I could do this with just CSS since I'm writing a theme for an Electron App where I can't (without going well out of my way) write custom JS for it.

答案1

得分: 1

以下是翻译好的部分:

也许你想要这个...

/* 包含单词 "logo" 的 元素的 class 属性 */
a[class~="logo"] {
padding: 2px;
}

所以...

[test~="key2-Value2"]

查看文档 这里

英文:

Perhaps you want this…

/* <a> elements whose class attribute contains the word "logo" */
a[class~="logo"] {
  padding: 2px;
}

So…

[test~="key2-Value2"]

See documentation here.

huangapple
  • 本文由 发表于 2023年4月17日 12:28:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定