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

View in English Always switch to English

Document:title 属性

基线
广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

document.title 属性用于获取或设置文档的当前标题。存在时,默认值为 <title> 的值。

值

包含文档标题的字符串。若已通过设置 document.title 覆盖标题,则包含该值。否则包含 <title> 元素中指定的标题。

js
document.title = newTitle;

newTitle 是文档的新标题。此次赋值会影响 document.title 的返回值、文档显示的标题(例如窗口或标签页的标题栏),也会影响文档的 DOM(例如 HTML 文档中 <title> 元素的内容)。

示例

假设文档的 <head> 如下所示:

html
<head>
  <meta charset="UTF-8" />
  <title>你好,世界!</title>
</head>
js
console.log(document.title); // "你好,世界!"
document.title = "再见,世界!"; // 页面标题已更改
console.log(document.title); // "再见,世界!"

规范

规范
HTML
# document.title

浏览器兼容性