<base>

<base>标签指定网页内部的所有相对 URL 的计算基准。整张网页只能有一个<base>标签,而且只能放在<head>里面。它是单独使用的标签,没有闭合标签,下面是一个例子。

  1. <head>
  2. <base href="https://www.example.com/files/" target="_blank">
  3. </head>

<base>标签的href属性给出计算的基准网址,target属性给出如何打开链接的说明(参见《链接》一章)。已知计算基准是https://www.example.com/files/,那么相对 URL foo.html,就可以转成绝对 URL https://www.example.com/files/foo.html

注意,<base>标签必须至少具有href属性或target属性之一。

  1. <base href="http://foo.com/app/">
  2. <base target="_blank">

一旦设置了<base>,就对整个网页都有效。如果要改变某个链接的行为,只能用绝对链接替代相对链接。尤其需要注意锚点,这时锚点也是针对<base>计算的,而不是针对当前网页的 URL。