位置:href 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

Location 接口的 href 属性是一个 stringifier,它返回一个包含完整 URL 的字符串,并允许更新 href。

设置 href 的值会导航到所提供的 URL。如果你想要重定向,请使用 location.replace()。与设置 href 属性值的区别在于,当使用 location.replace() 方法时,在导航到给定的 URL 后,当前页面将不会保存在会话 历史记录 中——这意味着用户将无法使用后退按钮导航回该页面。

字符串。

示例

js
// Lets imagine an <a id="myAnchor" href="https://mdn.org.cn/en-US/Location/href"> element is in the document
const anchor = document.getElementById("myAnchor");
const result = anchor.href; // Returns: 'https://mdn.org.cn/en-US/Location/href'

规范

规范
HTML
# dom-location-href-dev

浏览器兼容性