如何使用CSS指定所有具有特定模式的链接

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

How to specify all links with a certain pattern with CSS

问题

#p1 a, #p2 a, #p3 a, #p4 a, #p5 a, 等等。

我有成千上万个它们。

如何使用CSS指定它们所有?

尝试过 #p*
尝试过 id^="p"

英文:

#p1 a,#p2 a,#p3 a,#p4 a,#p5 a, etc.

I have thousands of them.

How can I specify all of them with CSS

tried #p*
tried id^="p"

答案1

得分: 3

Works for me.

[id^="p"] a {
  color: red;
}

[id^="z"] a {
  color: green;
  text-decoration: none;
}

[id^="z"] a:hover {
  color: blue;
  text-decoration: underline;
}
<p id="p1"><a href="">P One</a></p>
<p id="p2"><a href="">P Two</a></p>
<p id="p3"><a href="">P Three</a></p>
<p id="p4"><a href="">P Four</a></p>

<p id="z1"><a href="">Z One</a></p>
<p id="z2"><a href="">Z Two</a></p>
<p id="z3"><a href="">Z Three</a></p>
<p id="z4"><a href="">Z Four</a></p>

But I'm sure you could find a better selector than "id starts with p". Giving all the relevant links a class would seem a more robust solution.

英文:

Works for me.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

[id^=&quot;p&quot;] a {
  color: red;
}

[id^=&quot;z&quot;] a {
  color: green;
  text-decoration: none;
}

[id^=&quot;z&quot;] a:hover {
  color: blue;
  text-decoration: underline;
}

<!-- language: lang-html -->

&lt;p id=&quot;p1&quot;&gt;&lt;a href=&quot;&quot;&gt;P One&lt;/a&gt;&lt;/p&gt;
&lt;p id=&quot;p2&quot;&gt;&lt;a href=&quot;&quot;&gt;P Two&lt;/a&gt;&lt;/p&gt;
&lt;p id=&quot;p3&quot;&gt;&lt;a href=&quot;&quot;&gt;P Three&lt;/a&gt;&lt;/p&gt;
&lt;p id=&quot;p4&quot;&gt;&lt;a href=&quot;&quot;&gt;P Four&lt;/a&gt;&lt;/p&gt;

&lt;p id=&quot;z1&quot;&gt;&lt;a href=&quot;&quot;&gt;Z One&lt;/a&gt;&lt;/p&gt;
&lt;p id=&quot;z2&quot;&gt;&lt;a href=&quot;&quot;&gt;Z Two&lt;/a&gt;&lt;/p&gt;
&lt;p id=&quot;z3&quot;&gt;&lt;a href=&quot;&quot;&gt;Z Three&lt;/a&gt;&lt;/p&gt;
&lt;p id=&quot;z4&quot;&gt;&lt;a href=&quot;&quot;&gt;Z Four&lt;/a&gt;&lt;/p&gt;

<!-- end snippet -->

But I’m sure you could find a better selector than "id starts with p". Giving all the relevant links a class would seem a more robust solution.

答案2

得分: 0

你正在为&lt;a&gt;标签添加样式,所以在所有的id后面,像下面这样添加后代组合选择器**"a"**:

[id^="p"] a {
    color: "red";
}
英文:

You are styling on &lt;a&gt; tag so after all id put descendant combinators selector "a" like below

[id^=&quot;p&quot;] a{
 color: &quot;red&quot;;
}

huangapple
  • 本文由 发表于 2023年2月14日 08:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442378.html
匿名

发表评论

匿名网友

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

确定