此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

HTMLLinkElement:href 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

HTMLLinkElement 接口的 href 属性包含一个字符串,该字符串是与链接相关联的 URL。

它反映 <link> 元素的 href 属性。如果元素没有 href 属性,则此属性的值为空字符串("")。

备注: 每个 <link> 元素都必须包含 href 属性或 imagesrcset 属性中的一个或两个。这意味着,对于每个有效的 <link>,此属性或 imageSrcset 都不会为空。

包含 URL 的字符串,如果没有 href 元素,则为空字符串("")。

示例

html
<link rel="stylesheet" href="example.css" />
js
const link = document.getElementsByTag("link")[0];
console.log(link.href); // 'example.css'

规范

Specification
HTML
# dom-link-href

浏览器兼容性

参见