HTML - DOM 文件 title 屬性



HTML DOM 文件的 **title** 屬性用於設定或獲取文件的標題。title 屬性儲存有關標題的資訊。

語法

設定標題名稱
document.title = newTitle;
獲取標題名稱
document.title;

屬性

描述
newTitle 它表示文件的新標題。

返回值

它返回一個字串值,表示文件的標題。

HTML DOM 文件“title”屬性示例

以下示例說明如何設定和返回文件的標題。

返回標題

以下示例返回當前文件的標題名稱。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document title Property
    </title>
</head>
<body>
    <button onclick="fun()">Click me</button>
    <p>The title of the document :</p>
    <p id="title"></p>
    <script>
        function fun() {
            let x = document.title;
            document.getElementById("title").innerHTML = x;
        }
    </script>
</body>
</html>

設定新標題

以下示例設定當前文件的新標題名稱。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document title Property
    </title>
</head>
<body>
    <button onclick="fun()">Click me</button>
    <p>The new title of the document is:</p>
    <p id="title"></p>
    <script>
        function fun() {
            let x = document.title = "Title Property";
            document.getElementById("title").innerHTML = x;
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
title 是 1 是 12 是 1 是 1 是 12.1
html_dom_document_reference.htm
廣告