【问题标题】:css selector with not(rule AND rule ...)带有 not(rule AND rule ...) 的 css 选择器
【发布时间】:2014-12-18 21:17:36
【问题描述】:

我想创建一个只匹配外部链接的 CSS 选择器。问题是它似乎不适用于 :not() 中的多个规则。我不能使用多个 not (example: ":not():not()") because that becomes || instead of &&)。

内部链接

<a href="#!/something">...</a>

文档链接

<a href="/doc/.../binary/...">...</a>

外部链接

<a href="...whatever...">...</a>

与其他类型的链接不匹配的外部链接的 CSS 选择器。

a:not([href^="#!/"]):not([href="#"]):not([href^="/doc/"][href*="/binary/"]) {}

解释

a-tags NOT startswith hashbang || not hash || not (startswith /doc/ && contains /binary/)

在not()中似乎不可能有多个规则。这可以用其他方式解决吗?

HTML 来自 CMS,几乎没有属性。只允许使用“href”和“title”。如果我不必使用 js 循环遍历 HTML 并添加类或 rel 的解决方案,那就太好了。

【问题讨论】:

  • 如果您的外部链接以http// 开头,为什么不只使用[href^="http"][href^="//"] 来定位它们?
  • 不能用 :not() 完成。但如果我是你,我会为外部链接添加一个特定的 CSS 类,例如 class="extern"
  • @KhalidT。 rel="external" 更适合这个目的。
  • 链接也可以以 https:// 或 customcheme:// 或者甚至只是 / 开头,因为它可以是同一域中的另一个应用程序。有很多情况我想反转选择器。
  • 链接多个 :not()s 绝对是 &&,而不是 ||。

标签: css css-selectors


【解决方案1】:

你为什么不这样做

a[href*="//"]:not([href*="yoursite.com"]),
a[href^="#"],
a[href^="/doc/"],
a[href*="/binary/"]{    
  color: red;
}
a{
 color: green;
}
<a href="#!/something">HASH</a>

<a href="/doc/.../binary/...">Binary</a>

<a href="http://yoursite.com">Yoursite</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多