相关文章推荐
犯傻的小笼包  ·  遞增運算子(++) - ...·  1 月前    · 
跑龙套的青蛙  ·  HTTP headers - HTTP | MDN·  1 月前    · 
虚心的长颈鹿  ·  Object.freeze() - ...·  1 月前    · 
小眼睛的葡萄酒  ·  cmake - Make a test ...·  2 年前    · 
逼格高的花卷  ·  c++ - Why tellg() ...·  2 年前    · 
This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

* Some parts of this feature may have varying levels of support.

Learn more

The <a> HTML element (or anchor element), with its href attribute , creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address.

Content within each <a> should indicate the link's destination. If the href attribute is present, pressing the enter key while focused on the <a> element will activate it.

<li><a href="https://example.com">Website</a></li> <li><a href="mailto:m.bluth@example.com">Email</a></li> <li><a href="tel:+123456789">Phone</a></li> margin-bottom: 0.5rem;

Attributes

This element's attributes include the global attributes .

attributionsrc Experimental

Specifies that you want the browser to send an Attribution-Reporting-Eligible header. On the server-side this is used to trigger sending an Attribution-Reporting-Register-Source header in the response, to register a navigation-based attribution source .

The browser stores the source data associated with the navigation-based attribution source (as provided in the Attribution-Reporting-Register-Source response header) when the user clicks the link. See the Attribution Reporting API for more details.

There are two versions of this attribute that you can set:

Boolean, i.e., just the attributionsrc name. This specifies that you want the Attribution-Reporting-Eligible header sent to the same server as the href attribute points to. This is fine when you are handling the attribution source registration on the same server.

Value containing one or more URLs, for example:

html
attributionsrc="https://a.example/register-source
https://b.example/register-source"

This is useful in cases where the requested resource is not on a server you control, or you just want to handle registering the attribution source on a different server. In this case, you can specify one or more URLs as the value of attributionsrc. When the resource request occurs, the Attribution-Reporting-Eligible header will be sent to the URL(s) specified in attributionsrc in addition to the resource origin. These URLs can then respond with the Attribution-Reporting-Register-Source to complete registration.

Note: Specifying multiple URLs means that multiple attribution sources can be registered on the same feature. You might for example have different campaigns that you are trying to measure the success of, which involve generating different reports on different data.

<a> elements cannot be used as attribution triggers, only sources.

download

Causes the browser to treat the linked URL as a download. Can be used with or without a filename value:

Without a value, the browser will suggest a filename/extension, generated from various sources:

  • The Content-Disposition HTTP header
  • The final segment in the URL path
  • The media type (from the Content-Type header, the start of a data: URL, or Blob.type for a blob: URL)
  • filename: defining a value suggests it as the filename. / and \ characters are converted to underscores (_). Filesystems may forbid other characters in filenames, so browsers will adjust the suggested name if necessary.

    Note:

  • download only works for same-origin URLs, or the blob: and data: schemes.
  • How browsers treat downloads varies by browser, user settings, and other factors. The user may be prompted before a download starts, or the file may be saved automatically, or it may open automatically, either in an external application or in the browser itself.
  • If the Content-Disposition header has different information from the download attribute, resulting behavior may differ:
  • If the header specifies a filename, it takes priority over a filename specified in the download attribute.
  • If the header specifies a disposition of inline, Chrome and Firefox prioritize the attribute and treat it as a download. Old Firefox versions (before 82) prioritize the header and will display the content inline.
  • The URL that the hyperlink points to. Links are not restricted to HTTP-based URLs — they can use any URL scheme supported by browsers:

  • Telephone numbers with tel: URLs
  • Email addresses with mailto: URLs
  • SMS text messages with sms: URLs
  • Executable code with javascript: URLs
  • While web browsers may not support other URL schemes, websites can with registerProtocolHandler()
  • Moreover other URL features can locate specific parts of the resource, including:

  • Sections of a page with document fragments
  • Specific text portions with text fragments
  • Pieces of media files with media fragments
  • hreflang

    Hints at the human language of the linked URL. No built-in functionality. Allowed values are the same as the global lang attribute.

    A space-separated list of URLs. When the link is followed, the browser will send POST requests with the body PING to the URLs. Typically for tracking.

    referrerpolicy

    How much of the referrer to send when following the link.

  • no-referrer: The Referer header will not be sent.
  • no-referrer-when-downgrade: The Referer header will not be sent to origins without TLS (HTTPS).
  • origin: The sent referrer will be limited to the origin of the referring page: its scheme, host, and port.
  • origin-when-cross-origin: The referrer sent to other origins will be limited to the scheme, the host, and the port. Navigations on the same origin will still include the path.
  • same-origin: A referrer will be sent for same origin, but cross-origin requests will contain no referrer information.
  • strict-origin: Only send the origin of the document as the referrer when the protocol security level stays the same (HTTPS→HTTPS), but don't send it to a less secure destination (HTTPS→HTTP).
  • strict-origin-when-cross-origin (default): Send a full URL when performing a same-origin request, only send the origin when the protocol security level stays the same (HTTPS→HTTPS), and send no header to a less secure destination (HTTPS→HTTP).
  • unsafe-url: The referrer will include the origin and the path (but not the fragment, password, or username). This value is unsafe, because it leaks origins and paths from TLS-protected resources to insecure origins.
  • The relationship of the linked URL as space-separated link types.

    target

    Where to display the linked URL, as the name for a browsing context (a tab, window, or <iframe>). The following keywords have special meanings for where to load the URL:

  • _self: The current browsing context. (Default)
  • _blank: Usually a new tab, but users can configure browsers to open a new window instead.
  • _parent: The parent browsing context of the current one. If no parent, behaves as _self.
  • _top: The topmost browsing context. To be specific, this means the "highest" context that's an ancestor of the current one. If no ancestors, behaves as _self.
  • _unfencedTop: Allows embedded fenced frames to navigate the top-level frame (i.e., traversing beyond the root of the fenced frame, unlike other reserved destinations). Note that the navigation will still succeed if this is used outside of a fenced frame context, but it will not act like a reserved keyword.
  • Note: Setting target="_blank" on <a> elements implicitly provides the same rel behavior as setting rel="noopener" which does not set window.opener.

    Hints at the linked URL's format with a MIME type. No built-in functionality.

    Deprecated attributes

    charset Deprecated

    Hinted at the character encoding of the linked URL.

    Note: This attribute is deprecated and should not be used by authors. Use the HTTP Content-Type header on the linked URL.

    coords Deprecated

    Used with the shape attribute. A comma-separated list of coordinates.

    Deprecated

    Was required to define a possible target location in a page. In HTML 4.01, id and name could both be used on <a>, as long as they had identical values.

    Note: Use the global attribute id instead.

    Deprecated

    Specified a reverse link; the opposite of the rel attribute. Deprecated for being very confusing.

    shape Deprecated

    The shape of the hyperlink's region in an image map.

    Note: Use the <area> element for image maps instead.

    The content inside a link should indicate where the link goes, even out of context.

    A sadly common mistake is to only link the words "click here" or "here":

    html