Home  >  Article  >  Web Front-end  >  What is the usage of title attribute in javascript

What is the usage of title attribute in javascript

青灯夜游
青灯夜游Original
2021-12-08 16:50:493919browse

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
  • Settings ( Modify) Document title

document.title="需要设置的值"

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>

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!

Statement:
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