简体   繁体   English

为什么我们需要“a:link”? 为什么不只是“一个”?

[英]Why do we need “a:link”? Why not just “a”?

It seems the following variants produce identical results: 似乎以下变体产生相同的结果:

/* 1 */
a, a:visited, a:active { color: black; }
a:hover { color: red; }

/* 2 */
a, a:link, a:visited, a:active { color: black; }
a:hover { color: red; }

/* 3 */
a:link, a:visited, a:active { color: black; }
a:hover { color: red; }

Most guidance on the web uses 2 or 3. Why not go for the simplest variant which is 1? 网络上的大多数指导使用2或3.为什么不选择最简单的1? I cannot find a good reason to ever apply :link if all you need is one style for normal links and one for hover. 我找不到一个很好的理由申请:link如果您只需要一种普通链接样式和一种悬停样式。

Is it best-practice to not use :link ? 不使用是最佳做法:link Why or why not? 为什么或者为什么不?

I don't care whether the link is visited or not. 我不关心链接是否被访问过。 Both receive the same style. 两者都采用相同的风格。 I only care about hover or not hover. 我只关心悬停或不悬停。 This question is not about what the variants do - all do the same thing. 这个问题不是关于变体的作用 - 都做同样的事情。 It is about what the best variant is. 它是关于什么是最好的变种。 Is one of the variants faulty? 其中一个变种有缺陷吗? Which one is best-practice? 哪一个是最佳实践?

Because not every a is a link. 因为并非每a都是一个链接。

<a name="table_of_contents">Table of Contents</a>

isn't a link, it's an anchor that can be linked to with <a href="#table_of_contents"> . 不是链接,它是一个可以与<a href="#table_of_contents">链接的锚点。

a will match it, a:link won't. a会匹配它, a:link不会。

It is used to differentiate between simple anchors and anchors with href attributes. 它用于区分具有href属性的简单锚点和锚点。 See demo jsfiddle here . 在这里查看demo jsfiddle

<style>
a { color: red; }
a:link { color: blue; }
</style>
<a name="anchor">No href</a><br />
<a href="http://stackoverflow.com/">With href</a>

EDIT: For this reason, it is important to cover all cases in your CSS. 编辑:因此,重要的是涵盖CSS中的所有情况。 Option 2 is the only option that completely covers all cases. 选项2是完全涵盖所有案例的唯一选择。 If you do not have anchor elements without href attributes, you are safe with option 1. 如果没有没有href属性的锚元素,则选项1是安全的。

a:link is specifically for links that have not been visited. a:link专门用于未访问过的链接。 a applies to all <a> elements.as you said a适用于所有<a>元素。如你所说

I don't care whether the link is visited or not 我不关心链接是否被访问过

then you may avoid the use of a:link ...use of only a...a:hover...a:active will satisfy your need 然后你可以避免使用a:link ...只使用a...a:hover...a:active将满足你的需要

and also a:link wont work if there is no href in your anchor but a will do 还有a:link如果你的锚中没有href ,那么a:link不会工作a

I suppose you can use 我想你可以用

<a>

to create a button so that could produce alternate results... I always use a:link 创建一个按钮,以便产生替代结果...我总是使用a:link

It solely depends on your intention, so for your example, I would simply style all anchor elements one color and only change the style when the element is hovered. 它完全取决于你的意图,所以对于你的例子,我只是将所有锚元素设置为一种颜色,并且只在元素悬停时改变样式。

a {color: #000;}
a:hover {color: #f00;}

In your case, you are only changing the color of the link when it's hovered. 在您的情况下,您只是在链接悬停时更改链接的颜色。 You need to add the hover pseudo element after the base rule otherwise it would be overridden due to the cascading of the style sheet. 您需要在基本规则之后添加hover伪元素,否则由于样式表的级联而会覆盖它。

If you were to use all of the psuedo elements in this case and you wanted the only difference to be the hover it would look like this: 如果你在这种情况下使用所有的psuedo元素,并且你希望唯一的区别是悬停,它将如下所示:

a:link, a:visited, a:focus, a:active {color: #000;}
a:hover {color: #f00;}

The pseudo-class names are self explanatory: 伪类名称是自解释的:

  • :link - any unvisited link :链接 - 任何未访问的链接
  • :visited - any visited link :访问过 - 任何访问过的链接
  • :active - when the link is active, eg when it's clicked or activated with a keyboard event :active - 当链接处于活动状态时,例如,当用键盘事件单击或激活链接时
  • :focus - when the link gains focus, eg when a user tabs through the elements and it is the selected element :focus - 当链接获得焦点时,例如当用户选中元素并且它是所选元素时
  • :hover - when it's hovered or moused over :悬停 - 当它悬停或被鼠标悬停时

The benefit of using a pseudo-class is that it will have a higher specificity than just targeting the anchor element. 使用伪类的好处是它具有比仅仅针对锚元素更高的特异性。 However, in your case it may not be needed. 但是,在您的情况下,可能不需要它。

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

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