简体   繁体   English

将CSS属性选择器设置为:target

[英]Set CSS Atribute Selector to :target

Not sure if this is possible or not. 不知道这是否可能。

Trying to dynamically set an attribute selector to the current target (without the use of any JavaScript). 尝试动态地将属性选择器设置为当前目标(不使用任何JavaScript)。

Something like this css: 像这样的CSS:

.tab[href=#:target] {
  // styles here for active anchor tag
}

Where the html (in Jade syntax) is: html(使用Jade语法)为:

.tab-group
  a.tab(href="#tabA") Tab A
  a.tab(href="#tabB") Tab B
  a.tab(href="#tabC") Tab C

#tabA.target
.tab-content
  h2 Tab A
#tabB.target
.tab-content
  h2 Tab B
#tabC.target
.tab-content
  h2 Tab C

In other words: How to select the anchor link that set the current hashtag (with pure CSS, no JS). 换句话说:如何选择设置当前主题标签的锚链接(使用纯CSS,不使用JS)。

Why? 为什么? For CSS-only tabs using the :target pseudo selector approach that allow for the most flexible html structure possible. 对于仅使用CSS的选项卡,使用:target伪选择器方法可实现最灵活的html结构。

There are two different element types here related to the fragment identifier in the URL: there is the anchor element that the URL points to (the one with the same id as the hash in the URL) and there are the links that would point to the same URL as the current one. 此处与URL中的片段标识符相关的元素类型有两种:一种是URL指向的锚元素(一种与URL中的哈希值具有相同ID的元素),另一种是指向该元素的链接。与当前网址相同的网址。

:target selects the anchor element. :target选择锚元素。 In order to select the links, you would have to use Javascript to get the URL and then use some sort of selector like $=#YOURHASH (ends with) 为了选择链接,您将必须使用Javascript获取URL,然后使用某种选择器,例如$ =#YOURHASH(以结尾)

If you know the tabs in advance, you may create CSS rules like this: 如果您事先知道这些标签,则可以创建如下CSS规则:

.tabSomething .content a[href$=#Something] {
  // style for the links that lead to tab Something, because you know the hash is #Something for that tab
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM