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显示文档的标题?的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 如何使用JavaScript显示文档的标题?
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?