Home > Web Front-end > Front-end Q&A > What is the usage of title attribute in javascript

What is the usage of title attribute in javascript

青灯夜游
Release: 2021-12-08 16:50:49
Original
3958 people have browsed it

In javascript, the title attribute is used to set or return the title of the current document, that is, the text content in the HTML title element; directly use the "document.title" statement to return the title, use "document.title= "The value that needs to be set" statement can modify the title.

What is the usage of title attribute in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, the title attribute is used to set or return the title of the current document (the text content in the HTML title element).

Syntax:

  • Return document title

document.title
Copy after login
  • Settings ( Modify) Document title

document.title="需要设置的值"
Copy after login

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>旧文档标题</title>
	</head>
	<body>
		<input type="button" value="获取标题" id="btn1" />
		<input type="button" value="修改标题" id="btn2" />
		<script>
			function my(id) {
				return document.getElementById(id);
			}
			my("btn1").onclick = function() {
				var title = document.title;
				alert(title);
			}
			my("btn2").onclick = function() {
				document.title = "新文档标题";
				var title = document.title;
				alert("修改成功,新值为: " + title);
			}
		</script>
	</body>
</html>
Copy after login

What is the usage of title attribute in javascript

##[Related recommendations:

javascript learning tutorial

The above is the detailed content of What is the usage of title attribute in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template