HTMLAnchorElement:hash 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

HTMLAnchorElement.hash 属性返回一个字符串,该字符串包含一个 '#' 符号,后面跟着 URL 的片段标识符。

片段是百分号编码的。如果 URL 没有片段标识符,则此属性包含一个空字符串,即 ""

一个字符串。

示例

从锚点链接中获取哈希值

给定 HTML

html
<a id="myAnchor" href="/zh-CN/docs/Web/API/HTMLAnchorElement/hash#示例">
  示例
</a>

你可以像这样获取锚点哈希值:

js
const anchor = document.getElementById("myAnchor");
anchor.hash; // “#%E7%A4%BA%E4%BE%8B”

规范

Specification
HTML
# dom-hyperlink-hash-dev

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
hash

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

参见