【发布时间】: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