Html 将 alt 标签用于锚链接是否正确?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14850187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it correct to use alt tag for an anchor link?
提问by user2067736
Is it correct to use alt tag for an anchor link, something like
将 alt 标签用于锚链接是否正确,例如
<a href="#" class="test" alt="Something" src="sfasfs" ></a>
回答by unor
Such things are best answered by looking at the official specification:
这些事情最好通过查看官方规范来回答:
go to the specification: https://www.w3.org/TR/html5/
search for "
a
element": https://www.w3.org/TR/html5/text-level-semantics.html#the-a-elementcheck "Content attributes", which lists all allowed attributes for the
a
element:- Global attributes
href
target
download
rel
hreflang
type
check the linked "Global attributes": https://www.w3.org/TR/html5/dom.html#global-attributes
转到规范:https: //www.w3.org/TR/html5/
搜索“
a
元素”:https: //www.w3.org/TR/html5/text-level-semantics.html#the-a-element检查“内容属性”,其中列出了
a
元素的所有允许属性:- 全局属性
href
target
download
rel
hreflang
type
检查链接的“全局属性”:https: //www.w3.org/TR/html5/dom.html#global-attributes
As you will see, the alt
attribute is not allowed on the a
element.
Also you'd notice that the src
attribute isn't allowed either.
正如您将看到的,元素alt
上不允许使用该属性a
。
您还会注意到该src
属性也是不允许的。
By validating your HTML, errors like these are reported to you.
通过验证您的 HTML,会向您报告此类错误。
Note that the above is for HTML5, which is W3C's HTML standard from 2014. In 2016, HTML 5.1became the next HTML standard. Finding the allowed attributes works in the same way. You'll see that the a
element can have another attribute in HTML 5.1: rev
.
请注意,以上是针对HTML5 的,它是W3C 的 2014 年的 HTML 标准。2016 年,HTML 5.1成为下一个 HTML 标准。查找允许的属性的工作方式相同。您将看到该a
元素在 HTML 5.1: 中可以有另一个属性rev
。
You can find all HTML specifications (including the latest standard) on W3C's HTML Current Status.
您可以在W3C 的HTML 当前状态上找到所有 HTML 规范(包括最新标准)。
回答by tomis
For anchors, you should use title instead. alt is not valid atribute of a. See http://w3schools.com/tags/tag_a.asp
对于锚点,您应该改用标题。alt 不是 a 的有效属性。见http://w3schools.com/tags/tag_a.asp
回答by Pablo Pazos
"title" is widely implemented in browsers. Try:
“title”在浏览器中被广泛实现。尝试:
<a href="#" title="hello">asf</a>
回答by Jukka K. Korpela
No, an alt
attribute (it would be an attribute, not a tag) is not allowed for an a
element in any HTML specification or draft. And it does not seem to be recognized by any browser either as having any significance.
不,任何 HTML 规范或草案中alt
的a
元素都不允许使用属性(它将是一个属性,而不是标签)。而且它似乎也没有被任何浏览器识别为具有任何意义。
It's a bit mystery why people try to use it, then, but the probable explanation is that they are doing so in analog with alt
attribute for img
elements, expecting to see a “tooltip” on mouseover. There are two things wrong with this. First, each element has attributes of its own, defined in the specs for each element. Second, the “tooltip” rendering of alt
attributes in some ancient browsers is/was a quirk or even a bug, rather than something to be expected; the alt
attribute is supposed to be presented to the user if and only if the image itself is not presented, for whatever reason.
那么人们为什么尝试使用它有点神秘,但可能的解释是他们这样做是模拟元素的alt
属性img
,期望在鼠标悬停时看到“工具提示”。这有两个问题。首先,每个元素都有自己的属性,在每个元素的规范中定义。其次,alt
在一些古老的浏览器中,属性的“工具提示”呈现是/曾经是一个怪癖甚至是一个错误,而不是预期的;alt
当且仅当图像本身没有呈现时,该属性才应该呈现给用户,无论出于何种原因。
To create a “tooltip”, use the title
attribute instead or, much better, Google for "CSS tooltips" and use CSS-based tooltips of your preference (they can be characterized as hidden “layers” that become visible on mouseover).
要创建“工具提示”,请改用该title
属性,或者更好的是,谷歌搜索“CSS 工具提示”并使用您偏好的基于 CSS 的工具提示(它们可以被描述为在鼠标悬停时可见的隐藏“层”)。
回答by davidcondrey
You should use the title attribute for anchor tags if you wish to apply descriptive information similarly as you would for an alt attribute. The title attribute is valid on anchor tags and is serves no other purpose than providing information about the linked page.
如果您希望应用与 alt 属性类似的描述性信息,则应为锚标记使用 title 属性。title 属性对锚标记有效,除了提供有关链接页面的信息外,没有其他用途。
W3C recommends that the value of the title attribute should match the value of the title of the linked document but it's not mandatory.
W3C 建议 title 属性的值应与链接文档的标题值匹配,但这不是强制性的。
http://www.w3.org/MarkUp/1995-archive/Elements/A.html
http://www.w3.org/MarkUp/1995-archive/Elements/A.html
Alternatively, and likely to be more beneficial, you can use the ARIA accessibility attribute aria-label
(not to be confused with aria-labeledby
). aria-label
serves the same function as the alt attribute does for images but for non-image elements and includes some measure of optimization since your optimizing for screen readers.
或者,可能更有益的是,您可以使用 ARIA 可访问性属性aria-label
(不要与 混淆aria-labeledby
)。 aria-label
与 alt 属性对图像的作用相同,但用于非图像元素,并且包括一些优化措施,因为您针对屏幕阅读器进行了优化。
http://www.w3.org/WAI/GL/wiki/Using_aria-label_to_provide_labels_for_objects
http://www.w3.org/WAI/GL/wiki/Using_aria-label_to_provide_labels_for_objects
If you want to describe an anchor tag though, it's usually appropriate to use the rel or rev tag but your limited to specific values, they should not be used for human readable descriptions.
如果你想描述一个锚标签,通常使用 rel 或 rev 标签是合适的,但你仅限于特定的值,它们不应该用于人类可读的描述。
Rel serves to describe the relationship of the linked page to the current page. (e.g. if the linked page is next in a logical series it would be rel=next)
Rel 用于描述链接页面与当前页面的关系。(例如,如果链接页面是逻辑系列中的下一个,则为 rel=next)
The rev attribute is essentially the reverse relationship of the rel attribute. Rev describes the relationship of the current page to the linked page.
rev 属性本质上是 rel 属性的反向关系。Rev 描述了当前页面与链接页面的关系。
You can find a list of valid values here: http://microformats.org/wiki/existing-rel-values
您可以在此处找到有效值列表:http: //microformats.org/wiki/existing-rel-values
回答by Giovanni G. PY
I used title and it worked!
我使用了标题,它奏效了!
The title attribute gives the title of the link. With one exception, it is purely advisory. The value is text. The exception is for style sheet links, where the title attribute defines alternative style sheet sets.
title 属性给出了链接的标题。除了一个例外,它纯粹是建议性的。值为文本。样式表链接除外,其中 title 属性定义了替代样式表集。
<a class="navbar-brand" href="http://www.alberghierocastelnuovocilento.gov.it/sito/index.php" title="sito dell'Istituto Ancel Keys">A.K.</a>
回答by Bhojendra Rauniyar
I'm surprised to see all answers stating the use of alt
attribute in a
tag is not valid. This is absolutely wrong.
我很惊讶地看到所有答案都说明使用标签中的alt
属性无效。这是完全错误的。a
Html does not block you using any attributes:
Html 不会阻止您使用任何属性:
<a your-custom-attribute="value">Any attribute can be used</a>
If you ask if it is semantically correct to use alt
attribute in a
then I will say:
如果你问使用alt
属性在语义上是否正确,a
那么我会说:
NO. It is used to set image description <img alt="image description" />
.
不。用于设置图像描述<img alt="image description" />
。
It is a matter of what you'd do with the attributes. Here's an example:
这是你如何处理这些属性的问题。下面是一个例子:
a::after {
content: attr(color); /* attr can be used as content */
display: block;
color: white;
background-color: blue;
background-color: attr(color); /* This won't work */
display: none;
}
a:hover::after {
display: block;
}
[hidden] {
display: none;
}
<a href="#" color="red">Hover me!</a>
<a href="#" color="red" hidden>In some cases, it can be used to hide it!</a>
Again, if you ask if it is semantically correct to use custom attribute then I will say:
同样,如果您问使用自定义属性在语义上是否正确,那么我会说:
No. Use data-*
attributes for its semantic use.
否。将data-*
属性用于其语义用途。
Oops, question was asked in 2013.
糟糕,问题是在 2013 年提出的。