HTMLTitleElement: text 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

HTMLTitleElement 介面的 text 屬性將文件標題的子文字內容表示為一個字串。它包含 <title> 元素的文字內容;如果 <title> 元素中包含 HTML 標籤,則它們將作為字串值的一部分包含在內,而不是被解析為 HTML。

text 屬性設定值會替換 <title> 的全部文字內容。

字串。

示例

請看下面的示例

html
<!doctype html>
<html lang="en-US">
  <head>
    <title>
      Hello world! <span class="highlight">Isn't this wonderful</span> really?
    </title>
  </head>
  <body></body>
</html>
js
const title = document.querySelector("title");
console.log(title.text); // "Hello world! <span class="highlight">Isn't this wonderful</span> really?"
title.text = "Update the title";

正如您所見,span 標籤未被解析;<title> 元素的內��被視為純文字,並與它們在 title 元素中出現的完全相同地返回。

規範

規範
HTML
# dom-title-text-dev

瀏覽器相容性