<base>: The Document Base URL element

<base> HTML 元素指定用于文档中所有相对 URL 的基本 URL。文档中只能有一个 <base> 元素。

¥The <base> HTML element specifies the base URL to use for all relative URLs in a document. There can be only one <base> element in a document.

文档使用的基本 URL 可以通过带有 Node.baseURI 的脚本访问。如果文档没有 <base> 元素,则 baseURI 默认为 location.href

¥A document's used base URL can be accessed by scripts with Node.baseURI. If the document has no <base> elements, then baseURI defaults to location.href.

属性

¥Attributes

该元素的属性包括 全局属性

¥This element's attributes include the global attributes.

警告:<base> 元素必须具有 href 属性、target 属性或两者。如果至少指定了这些属性之一,则 <base> 元素必须位于属性值为 URL 的其他元素之前,例如 <link>href 属性。

¥Warning: A <base> element must have an href attribute, a target attribute, or both. If at least one of these attributes are specified, the <base> element must come before other elements with attribute values that are URLs, such as a <link>'s href attribute.

href

整个文档中用于相对 URL 的基本 URL。允许使用绝对和相对 URL。不允许使用 data:javascript: URL。

target

默认 browsing context 的关键字或作者定义的名称,用于显示从 <a><area><form> 元素导航的结果,而无需显式 target 属性。以下关键字具有特殊含义:

  • _self(默认):在当前浏览上下文中显示结果。
  • _blank:在新的、未命名的浏览上下文中显示结果。
  • _parent:如果当前页面位于框架内,则在当前页面的父浏览上下文中显示结果。如果没有父级,则与 _self 相同。
  • _top:在最顶层的浏览上下文(当前浏览上下文的祖级并且没有父浏览上下文)中显示结果。如果没有父级,则与 _self 相同。

使用说明

¥Usage notes

多个 <base> 元素

¥Multiple <base> elements

如果使用多个 <base> 元素,则仅遵循第一个 href 和第一个 target - 所有其他元素都将被忽略。

¥If multiple <base> elements are used, only the first href and first target are obeyed — all others are ignored.

页内锚点

¥In-page anchors

指向文档中片段的链接 - 例如 <a href="#some-id"> — 使用 <base> 进行解析,触发对附加片段的基本 URL 的 HTTP 请求。

¥Links pointing to a fragment in the document — e.g. <a href="#some-id"> — are resolved with the <base>, triggering an HTTP request to the base URL with the fragment attached.

例如,给定 <base href="https://example.com/"> 和此链接:<a href="#anchor">To anchor</a>。该链接指向 https://example.com/#anchor

¥For example, given <base href="https://example.com/"> and this link: <a href="#anchor">To anchor</a>. The link points to https://example.com/#anchor.

打开图

¥Open Graph

打开图 标签不承认 <base>,并且应始终具有完整的绝对 URL。例如:

¥Open Graph tags do not acknowledge <base>, and should always have full absolute URLs. For example:

html
<meta property="og:image" content="https://example.com/thumbnail.jpg" />

示例

¥Examples

html
<base href="https://www.example.com/" />
<base target="_blank" />
<base target="_top" href="https://example.com/" />

技术总结

¥Technical summary

内容类别 元数据内容。
允许的内容 没有任何;这是 void element
标签遗漏 必须有开始标记,并且不能有结束标记。
允许的父级 不包含另一个 <base> 元素的 <head>
隐式 ARIA 角色 没有对应的角色
允许的 ARIA 角色 不允许 role
DOM 接口 HTMLBaseElement

规范

Specification
HTML Standard
# the-base-element

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility