--- title: HTMLAnchorElement slug: Web/API/HTMLAnchorElement translation_of: Web/API/HTMLAnchorElement ---
{{APIRef("HTML DOM")}}

HTMLAnchorElement 接口表示超链接元素,并提供一些特别的属性和方法(除了那些继承自普通 {{domxref("HTMLElement")}}对象接口的之外)以用于操作这些元素的布局和显示。

属性(Properties)

继承其父类 {{domxref("HTMLElement")}} 的属性,并实现 {{domxref("URLUtils")}} 中(定义)的(属性)。

{{domxref("HTMLAnchorElement.accessKey")}}
是一个代表了单个字符的 {{domxref("DOMString")}},单个字符可以切换输入焦点到超链接。
{{domxref("HTMLAnchorElement.charset")}} {{obsolete_inline}}
Is a {{domxref("DOMString")}} representing the character encoding of the linked resource.
{{domxref("HTMLAnchorElement.coords")}} {{obsolete_inline}}
Is a {{domxref("DOMString")}} representing a comma-separated list of coordinates.
{{domxref("HTMLAnchorElement.download")}} {{experimental_inline}}
Is a {{domxref("DOMString")}} indicating that the linked resource is intended to be downloaded rather than displayed in the browser. The value represent the proposed name of the file. If the name is not a valid filename of the underlying OS, browser will adapt it. The value is a URL with a scheme like http:, file:, data: or even blob: (created with {{domxref("URL.createObjectURL")}}).
{{domxref("URLUtils.hash")}}
Is a {{domxref("DOMString")}} representing the fragment identifier, including the leading hash mark ('#'), if any, in the referenced URL.
{{domxref("URLUtils.host")}}
Is a {{domxref("DOMString")}} representing the hostname and port (if it's not the default port) in the referenced URL.
{{domxref("URLUtils.hostname")}}
Is a {{domxref("DOMString")}} representing the hostname in the referenced URL.
{{domxref("URLUtils.href")}}
Is a {{domxref("DOMString")}} that reflects the {{htmlattrxref("href", "a")}} HTML attribute, containing a valid URL of a linked resource.
{{domxref("HTMLAnchorElement.hreflang")}}
Is a {{domxref("DOMString")}} that reflects the {{htmlattrxref("hreflang", "a")}} HTML attribute, indicating the language of the linked resource.
{{domxref("HTMLAnchorElement.media")}}
Is a {{domxref("DOMString")}} that reflects the {{htmlattrxref("media", "a")}} HTML attribute, indicating the intended media for the linked resource.
{{domxref("HTMLAnchorElement.name")}} {{obsolete_inline}}
Is a {{domxref("DOMString")}} representing the anchor name.
{{domxref("URLUtils.passport")}}
Is a {{domxref("DOMString")}} 包含指定域名的密码。
{{domxref("URLUtils.origin")}} {{readonlyInline}}
Returns a {{domxref("DOMString")}} containing the origin of the URL, that is its scheme, its domain and its port.
{{domxref("URLUtils.pathname")}}
Is a {{domxref("DOMString")}} representing the path name component, if any, of the referenced URL.
{{domxref("URLUtils.port")}}
Is a {{domxref("DOMString")}} representing the port component, if any, of the referenced URL.
{{domxref("URLUtils.protocol")}}
Is a {{domxref("DOMString")}} representing the protocol component, including trailing colon (':'), of the referenced URL.
{{domxref("HTMLAnchorElement.rel")}}
Is a {{domxref("DOMString")}} that reflects the {{htmlattrxref("rel", "a")}} HTML attribute, specifying the relationship of the target object to the linked object.
{{domxref("HTMLAnchorElement.relList")}} {{readonlyInline}}
Returns a {{domxref("DOMTokenList")}} that reflects the {{htmlattrxref("rel", "a")}} HTML attribute, as a list of tokens.
{{domxref("HTMLAnchorElement.rev")}} {{obsolete_inline}}
Is a {{domxref("DOMString")}} representing that the {{htmlattrxref("rev", "a")}} HTML attribute, specifying the relationship of the link object to the target object.
{{domxref("URLUtils.search")}}
Is a {{domxref("DOMString")}} representing tThe search element, including leading question mark ('?'), if any, of the referenced URL.
{{domxref("HTMLAnchorElement.shape")}} {{obsolete_inline}}
Is a {{domxref("DOMString")}} representing the shape of the active area.
{{domxref("HTMLAnchorElement.tabindex")}}
Is a long containing the position of the element in the tabbing navigation order for the current document.
{{domxref("HTMLAnchorElement.target")}}
Is a {{domxref("DOMString")}} that reflects the {{htmlattrxref("target", "a")}} HTML attribute, indicating where to display the linked resource.
{{domxref("HTMLAnchorElement.text")}}
Is a {{domxref("DOMString")}} being a synonym for the {{domxref("Node.textContent")}} property.
{{domxref("HTMLAnchorElement.type")}}
Is a {{domxref("DOMString")}} that reflects the {{htmlattrxref("type", "a")}} HTML attribute, indicating the MIME type of the linked resource.
{{domxref("URLUtils.username")}}
Is a {{domxref("DOMString")}} containing the username specified before the domain name.

Methods

Inherits methods from its parent, {{domxref("HTMLElement")}}, and implements those from {{domxref("URLUtils")}}.

{{domxref("HTMLElement.blur()")}}
Removes the  keyboard focus from the current element.
{{domxref("HTMLElement.focus()")}}
Gives the keyboard focus to the current element.
{{domxref("URLUtils.toString()")}}
Returns a {{domxref("DOMString")}} containing the whole URL. It is a synonym for {{domxref("URLUtils.href")}}, though it can't be used to modify the value.

The blur() and focus() methods are inherited from {{domxref("HTMLElement")}} from HTML5 on, but were defined on HTMLAnchorElement in DOM Level 2 HTML and earlier specifications.

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', "text-level-semantics.html#the-a-element", "HTMLAnchorElement")}} {{Spec2('HTML WHATWG')}} The following property has been added: download.
Technically, the URL-related properties, media, host, hostname, pathname, port, protocol, search, and hash, have been moved to the {{domxref("URLUtils")}} interface, and HTMLAreaElement implements this interface.
{{SpecName('HTML5 W3C', "text-level-semantics.html#the-a-element", "HTMLAnchorElement")}} {{Spec2('HTML5 W3C')}} The methods blur() and focus(), as well as the properties tabindex and accessKey, are now defined on {{domxref("HTMLElement")}}.
The following properties are now obsolete: charset, coords, name, rev, and shape.
The following properties have been added: hash, host, hostname, media, pathname, port, protocol, relList, search, and text.
{{SpecName('DOM2 HTML', 'html.html#ID-48250443', 'HTMLAnchorElement')}} {{Spec2('DOM2 HTML')}} No change from {{SpecName("DOM1")}}.
{{SpecName('DOM1', 'level-one-html.html#ID-48250443', 'HTMLAnchorElement')}} {{Spec2('DOM1')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatGeckoDesktop(1.0)}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
download 14 20 {{CompatUnknown}} 15 {{CompatUnknown}}
username, password, and origin {{CompatUnknown}} {{CompatGeckoDesktop("26.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatGeckoMobile(1.0)}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
download {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
username, password, and origin {{CompatUnknown}} {{CompatGeckoMobile("26.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also