This translation is incomplete. Please help translate this article from English.
Resum
L'element HTML <a>
(de l'Anglès Anchor) defineix un hipervincle, el name target de la destinació per un hipervincle, o ambdós.
- Categories de contingut Contingut de flux, phrasing content, contingut interactiu, contingut palpable.
- Contingut permès Transparent, conté o contingut de flux o phrasing content .
- Omissió de l'etiqueta Cap, tant l'etiqueta inicial com l’etiqueta final són obligatòries
- Elements pares permesos Qualsevol element que accepti phrasing content, o qualsevol element que accepti contingut de flux.
- Interfície DOM
HTMLAnchorElement
Atributs
Aquest element inclou els atributs globals.
download
HTML5- Aquest atribut, en cas que hi sigui present, indica que l'autor pretén que l'hipervincle sigui usat per descarregar un recurs de forma que quan l'usuari cliqui en l'enllaç se'ls demanarà que de guardar-ho com un arxiu local. Si l'atribut té un valor, el valor s'utilitza com el nom del fitxer precarregada al Save prompt que s'obre quan l'usuari fa clic a l'enllaç (l'usuari pot canviar el nom abans de guardar-és clar). No hi ha restriccions en valors permesos (though
/
and\
will be converted to underscores, preventing specific path hints), però s'hauria de considerar que la majoria dels sistemes de fitxers tenen limitacions amb regard to what punctuation is supported en noms d'arxius, i els navegadors are likely to adjust file names accordingly.Nota:
- Can be used with
blob:
URLs idata:
URLs, per fer més més fàcil pels usuaris per descarregar contingut que és generat to download content that is generated programmatically using JavaScript (e.g. a picture created using an online drawing Web app). - If the HTTP header
Content-Disposition:
is present and gives a different filename than this attribute, the HTTP header has priority over this attribute. - If this attribute is present and
Content-Disposition:
is set toinline
, Firefox gives priority toContent-Disposition
, like for the filename case, while Chrome gives priority to thedownload
attribute. - En Firefox 20 aquest atribut només s'honra pels enllaços als recursos amb el mateix origen.
- Can be used with
href
- This was the single required attribute for anchors defining a hypertext source link, but is no longer required in HTML5. Omitting this attribute creates a placeholder link. The
href
attribute indicates the link target, either a URL or a URL fragment. A URL fragment is a name preceded by a hash mark (#), which specifies an internal target location (an ID) within the current document. URLs are not restricted to Web (HTTP)-based documents. URLs might use any protocol supported by the browser. For example,file
,ftp
, andmailto
work in most user agents.Note: You can use the special fragment "top" to create a link back to the top of the page; for example
<a href="#top">Return to top</a>
. This behavior is specified by HTML5. media
HTML5- This attribute specifies the media which the linked resource applies to. Its value must be a media query. This attribute is mainly useful when linking to external stylesheets by allowing the user agent to pick the best adapted one for the device it runs on.
Usage note:
- In HTML 4, only simple white-space-separated list of media description literals, i.e. media types and groups, where defined and allowed as values for this attribute, like
print
,screen
,aural
,braille
, ... HTML 5 extended this to any kind of media queries, which are a superset of the allowed values of HTML 4. - Browsers not supporting the CSS3 Media Queries won't necessarily recognize the adequate link; do not forget to set fallback links, the restricted set of media queries defined in HTML 4.
- In HTML 4, only simple white-space-separated list of media description literals, i.e. media types and groups, where defined and allowed as values for this attribute, like
ping
HTML5- The 'ping' attribute, if present, sends the URLs of the resources a notification/ping if the user follows the hyperlink.
rel
- For anchors containing the href attribute, this attribute specifies the relationship of the target object to the link object. The value is a comma-separated list of link types values. The values and their semantics will be registered by some authority that might have meaning to the document author. The default relationship, if no other is given, is void. Use this attribute only if the href attribute is present.
target
- This attribute specifies where to display the linked resource. In HTML4, this is the name of, or a keyword for, a frame. In HTML5, it is a name of, or keyword for, a browsing context (for example, tab, window, or inline frame). The following keywords have special meanings:
_self
: Load the response into the same HTML4 frame (or HTML5 browsing context) as the current one. This value is the default if the attribute is not specified._blank
: Load the response into a new unnamed HTML4 window or HTML5 browsing context._parent
: Load the response into the HTML4 frameset parent of the current frame or HTML5 parent browsing context of the current one. If there is no parent, this option behaves the same way as_self
._top
: In HTML4: Load the response into the full, original window, canceling all other frames. In HTML5: Load the response into the top-level browsing context (that is, the browsing context that is an ancestor of the current one, and has no parent). If there is no parent, this option behaves the same way as_self
.
charset
Obsolete since HTML5- This attribute defines the character encoding of the linked resource. The value is a space- and/or comma-delimited list of character sets as defined in RFC 2045. The default value is ISO-8859-1.
Usage note: This attribute is obsolete in HTML5 and should not be used by authors. To achieve its effect, use the HTTP Content-Type header on the linked resource.
coords
HTML 4 only, Obsolete since HTML5- For use with object shapes, this attribute uses a comma-separated list of numbers to define the coordinates of the object on the page.
datafld
- This attribute specifies the column name from that data source object that supplies the bound data.
Usage note: This attribute is non-standard and should not be used by authors. To achieve its effect, use scripting and a mechanism such as XMLHttpRequest to populate the page dynamically
Support Gecko Presto WebKit Trident Not implemented Not implemented Not implemented IE4, IE5, IE6, IE7 (Removed in IE8) Normative document Microsoft's Data Binding: dataFld Property (MSDN) datasrc
- This attribute indicates the ID of the data source object that supplies the data that is bound to this element.
Usage note: This attribute is non-standard and should not be used by authors. To achieve its effect, use scripting and a mechanism such as XMLHttpRequest to populate the page dynamically.
Support Gecko Presto WebKit Trident Not implemented Not implemented Not implemented IE4, IE5, IE6, IE7 (Removed in IE8) Normative document Microsoft's Data Binding: dataSrc Property (MSDN) hreflang
- This attribute indicates the language of the linked resource. It is purely advisory. Allowed values are determined by BCP47 for HTML5 and by RFC1766 for HTML4. Use this attribute only if the
href
attribute is present. methods
- The value of this attribute provides information about the functions that might be performed on an object. The values generally are given by the HTTP protocol when it is used, but it might (for similar reasons as for the title attribute) be useful to include advisory information in advance in the link. For example, the browser might choose a different rendering of a link as a function of the methods specified; something that is searchable might get a different icon, or an outside link might render with an indication of leaving the current site. This attribute is not well understood nor supported, even by the defining browser, Internet Explorer 4. Methods Property (MSDN)
name
HTML 4 only, Obsolete since HTML5- This attribute is required in an anchor defining a target location within a page. A value for name is similar to a value for the id core attribute and should be an alphanumeric identifier unique to the document. Under the HTML 4.01 specification, id and name both can be used with the <a> element as long as they have identical values.
Usage note: This attribute is obsolete in HTML5, use global attribute id instead.
rev
HTML 4 only, Obsolete since HTML5- This attribute specifies a reverse link, the inverse relationship of the rel attribute. It is useful for indicating where an object came from, such as the author of a document.
shape
HTML 4 only, Obsolete since HTML5- This attribute is used to define a selectable region for hypertext source links associated with a figure to create an image map. The values for the attribute are
circle
,default
,polygon
, andrect
. The format of the coords attribute depends on the value of shape. Forcircle
, the value isx,y,r
wherex
andy
are the pixel coordinates for the center of the circle andr
is the radius value in pixels. Forrect
, the coords attribute should bex,y,w,h
. Thex,y
values define the upper-left-hand corner of the rectangle, whilew
andh
define the width and height respectively. A value ofpolygon
for shape requiresx1,y1,x2,y2,...
values for coords. Each of thex,y
pairs defines a point in the polygon, with successive points being joined by straight lines and the last point joined to the first. The valuedefault
for shape requires that the entire enclosed area, typically an image, be used. type
- This attribute specifies the media type in the form of a MIME type for the link target. Generally, this is provided strictly as advisory information; however, in the future a browser might add a small icon for multimedia types. For example, a browser might add a small speaker icon when type is set to audio/wav. For a complete list of recognized MIME types, see https://www.w3.org/TR/html4/references.html#ref-MIMETYPES. Use this attribute only if the href attribute is present.
urn
- This supposedly Microsoft-supported attribute relates a uniform resource name (URN) with the link. While it is based on standards work years back, the meaning of URNs is still not well defined, so this attribute is meaningless. urn Property (MSDN)
Example: Linking to an external location
<!-- anchor linking to external file --> <a href="https://www.mozilla.com/"> External Link </a>
Result
Example: Creating a clickable image
This small example use an image to link to the MDN home page. The home page will open in a new browsing context, that is a new page or a new tab.
<a href="https://developer.mozilla.org/en-US/" target="_blank"> <img src="https://mdn.mozillademos.org/files/6851/mdn_logo.png" alt="MDN logo" /> </a>
Result
Example: Crear un enllaç d'email
És habitual crear botons o enllaços que obriran el programa d'email de l'usuari per permetrels enviar un missatge nou. Això s'aconsegueix fent servir l'enllaç mailto
. Aquí hi ha un exemple simple:
<a href="mailto:[email protected]">Send email to nowhere</a>
This results in a link that looks like this: Send email to nowhere.
Per més informació sobre el mailto
URL scheme, comaracoom incloure el subjecte, el cos o altre contingut predeterminat, veure Enllaços d'email o RFC 6068.
Exemple: Fer servir l'atribut de descàrrega per guardar un canvas com a PNG
If you want to allow a user to download an HTML Canvas as an image you can create a link with a download attribute and the canvas data as a file URL:
var link = document.createElement('a'); link.innerHTML = 'download image'; link.addEventListener('click', function(ev) { link.href = canvas.toDataURL(); link.download = "mypainting.png"; }, false); document.body.appendChild(link);
Pots veure això en acció https://jsfiddle.net/codepo8/V6ufG/2/
Especificacions
Especificació | Estat | Comentaris |
---|---|---|
WHATWG HTML Living Standard The definition of '<a>' in that specification. |
Living Standard | |
HTML5 The definition of '<a>' in that specification. |
Recommendation | |
HTML 4.01 Specification The definition of '<a>' in that specification. |
Recommendation |
Compatibilitat amb navegadors
Característica |
Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | 1.0 (1.7 or earlier) | (Yes) | (Yes) | (Yes) |
href="#top" |
(Yes) | 10.0 (10.0) | (Yes) | (Yes) | (Yes) |
download |
14 | 20.0 (20.0) | Not supported | 15 | Not supported |
ping |
(Yes) | Desactivat per defecte | Not supported | (Yes) | Not supported |
Característica | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | (Yes) | 1.0 (1.0) | (Yes) | (Yes) | (Yes) |
href="#top" |
(Yes) | 10.0 (10.0) | (Yes) | (Yes) | (Yes) |
download |
(Yes) | 20.0 (20.0) | Not supported | ? | Not supported |
ping |
Desactivat per defecte |
Clicking and focus
Whether clicking on an <a>
causes it to (by default) become focused varies by browser and OS.
Navegadors d'escriptori | Windows 8.1 | OS X 10.9 |
---|---|---|
Firefox 30.0 | Sí | Sí |
Chrome ≥39 (Chromium bug 388666) |
Sí | Sí |
Safari 7.0.5 | N/A | Només quan tabindex està definit |
Internet Explorer 11 | Sí | N/A |
Presto (Opera 12) | Sí | Sí |
Mobile Browsers | iOS 7.1.2 | Android 4.4.4 |
---|---|---|
Safari Mobile | Només quan tabindex està definit |
N/A |
Chrome 35 | ??? | Només quan tabindex està definit |
Notes
The following are reserved browser key bindings for the two major browsers and should not be used as values to accesskey: a, c, e, f, g, h, v, left arrow, and right arrow.
HTML 3.2 defineix només name, href, rel, rev, i title.
L'atribut target no està definit en navegadors que no suporten frames, com ara la generació de navegadors Netscape 1. Furthermore, target is not allowed under strict variants of XHTML but is limited to frameset or transitional forms.
Recomanacions per JavaScript
It is often the case that an anchor tag is used with the onclick
event. In order to prevent the page from refreshing, href is often set to either "#" or "javascript:void(0)". Both of these values can lead to some unexpected errors when copying links and opening links in a new tab and/or window. Be aware of this for usability reasons, and when users do use anchor tags and you prevent default behavior.
Vegeu també
- Altres elements pertanyents a text-level semantics:
<abbr>
,<em>
,<strong>
,<small>
,<cite>
,<q>
,<dfn>
,<time>
,<code>
,<var>
,<samp>
,<kbd>
,<sub>
,<sup>
,<b>
,<i>
,<mark>
,<ruby>
,<rp>
,<rt>
,<bdo>
,<span>
,<br>
,<wbr>
.