如何使用JavaScript显示文档的标题?

PHPz
PHPz 转载
2023-08-24 09:49:32 832浏览

如何使用JavaScript显示文档的标题?

In this tutorial, we will understand two approaches to display the document's title using JavaScript.

Using document.title Property

One of the most used methods in JavaScript to access an HTML document’s title is using a document.title property. In the following example, you will learn to get access to the title. After accessing the title in JavaScript, you can manipulate it and change the way it is displayed on a website.

语法

在这里,您可以看到如何使用onClick方法,我们可以设置文档中段落的innerHTML。document.title用于获取标题,并在单击指定按钮时显示标题。

title.innerHTML = document.title;

算法

Step 1 − Write a title in an HTML document file.

第二步 - 使用onClick方法创建一个按钮,以便获取标题。

步骤 3 − 创建一个段落标签,用于显示抓取的标题。

第四步 - 设置文档中不同元素所需的变量。

Step 5 − Create a function for the button onClick.

Step 6 − Give the paragraph tag’s variable innerHTML using the document.title method.

Example 1

的中文翻译为:

示例 1

您可以在下面看到,我们如何在HTML文件中不给它任何id或class的情况下访问文档的标题。可以使用document.title来访问标题。

<html>
   <head>
      <title> This is the title accessed from the document </title>
   </head>
   <body>
      <h3> Please click the button to get the title of the document</h3>
      <button id="titleBtn" onClick="titleButton()">Check Title</button>
      <p id="result"></p>
      <script>
         var paraTitle = document.getElementById('result');
         function titleButton() {
            paraTitle.innerHTML = document.title;
            document.getElementById('titleBtn').style.visibility = 'hidden';
         }
      </script>
   </body>
</html>

Using the etElementsByTagName() Method

通常,我们需要使用JavaScript函数来获取标题,以便在不同平台上进行操作。在这种方法中,您将学习如何使用document.getElementsByTagName()属性来获取标题。该方法接受一个标签名称作为参数,并返回具有指定标签名称的所有元素的集合。

语法

document.getElementsByTagName("title")[idx];

Here “title” is the parameter to the method.

该方法将返回所有带有标签“title”的元素的集合。

We need to apply indexing to the received collection to get the different elements. Here idx is the index of the title. For example, to get the first title we set the idx to 0, and in the same way to get the second title we set the idx to 1.

算法

Step 1 − Write something within the title tags of the HTML document.

第二步 - 创建按钮标签以便能够使用onClick方法。

Step 3 − Create paragraph tags and give them an id to get access in JavaScript.

第四步 - 您可以为文档中的所有重要元素分配id或class。

Step 5 − Create a different variable that can grab the required elements.

第六步 - 创建一个onClick方法的函数。

第7步 - 应该使用tagName()属性给为段落创建的变量设置innerHTML。

Example 2

在这个例子中,我们将通过标签名选择标题。您将学习如何使用document.getElementsByTagName()方法从HTML文档中快速获取标题。我们在HTML文档中添加了两个标题。我们使用两个按钮找到这两个标题。

<html>
   <head>
      <title> This is the first title accessed using index 0. </title>
      <title> This is the second title accessed using index 1.</title>
   </head>
   <body>
      <h3>Getting the Title of the document using Tag Name. </h3>
      <button id="titleBtn" onClick="titleButton()">Check First Title</button>
      <button id="secondBtn" onClick="secondButton()">Check Second Title</button>
      <p id="paraTitle"> </p>
      <script>
         var paraTitle = document.getElementById('paraTitle');
         function titleButton() {
            var title = document.getElementsByTagName("title")[0];
            paraTitle.innerHTML = title.innerHTML;
         }
         function secondButton() {
            var title = document.getElementsByTagName("title")[1];
            paraTitle.innerHTML = title.innerHTML;
         }
      </script>
   </body>
</html>

在这里,您可以看到我们添加了两个按钮,用于显示文档中的不同标题。通过这个输出,您可以理解在tagName()属性后添加0索引可以帮助获取第一个标题。

document.title属性和getElementByTagName()方法都用于访问文档的标题。您可以在JavaScript中尝试这两种方法,然后选择首选方法。如果您想要操作文档标题的行为,那么使用JavaScript访问标题可能是一个很好的起点。

以上就是如何使用JavaScript显示文档的标题?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除