HTML <base> 标签用于指定基本 URL。<base> 标签中应该有 href 和 target 属性。
此外,此元素指定必须如何打开当前文档中的链接。HTML <head> 元素包含唯一一个可以在页面上使用的 <base> 标签。它必须尽快插入,因为它的作用延伸到它被赋予的地方。
如果您使用多个 <base> 元素,则仅遵循第一个 href 和 target 属性。其余的将不予考虑。
语法
<base href="" target="">
属性
<base> 标签支持 全局属性 。并接受下面列出的一些特定属性。
属性 | 值 | 描述 |
---|---|---|
href | URL | 定义页面的 URL 或链接转到的锚点的名称。 |
target | _blank _parent _self _top |
定义在何处打开目标 URL。 |
<base> 标签示例
下面的示例将说明<base> 标签的用法。在何处、何时以及如何使用<base> 标签来指定 URL。
<base> 标签默认 URL
以下是 <base> 标签用法的基本示例。
<!DOCTYPE html>
<html>
<head>
<title>HTML base Tag</title>
<base href="https://www.qikepu.com" />
</head>
<body>
<img src="/cg/images/logo.png" />
</body>
</html>
<base> 标签定义所有链接的默认目标
让我们看一下以下示例,我们将在其中为页面上的所有 URL 设置默认目标。
<!DOCTYPE html>
<html>
<head>
<base href="https://www.qikepu.com/index.htm">
</head>
<body>
<p>
<a href="/html/index.htm">
<img src="/cg/images/logo.png"
height="39"
alt="logo">
</a>
<br>
QikepuCom originated from the idea that
there exists a class of readers who respond
better to online content and prefer to learn new
skills at their own pace from the comforts of their
drawing rooms.
</p>
</body>
</html>
<base> 标签指定多个相对链接的 URL
考虑另一种情况,我们要采用基本 URL,所有相对链接都将其视为起始 URL,并在同一窗口中打开。
<!DOCTYPE html>
<html>
<head>
<title>HTML base Tag</title>
<base href="https://www.qikepu.com/index.htm">
</head>
<body >
<h2>List of QikepuCom</h2>
<ul>
<li><a href="/html/index.htm">HTML QikepuCom</a></li>
<li><a href="/css/index.htm">CSS QikepuCom</a></li>
</ul>
</body>
</html>
支持的浏览器
浏览器 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
是否支持 | Yes | Yes | Yes | Yes | Yes |