Home > Web Front-end > Front-end Q&A > How to modify the title of html in js

How to modify the title of html in js

青灯夜游
Release: 2023-01-07 11:47:13
Original
7182 people have browsed it

JS method to modify html title: 1. Use the "document.title="value that needs to be set";" statement; 2. Use "$('title').html('value that needs to be set' )" or "$('title').text('value to be set')" statement.

How to modify the title of html in js

The operating environment of this tutorial: windows7 system, javascript1.8.5&&jquery1.7.2 version, Dell G3 computer.

Method 1: document.title method

After testing, the value of title can also be set through document.title.

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

Example

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>hello</title>
	</head>
	<body>
		<script type="text/javascript">
			document.title = &#39;php中文网&#39;;
		</script>
	</body>
</html>
Copy after login

How to modify the title of html in js

Method 2: Using jQuery’s html() or text() method

Of course, if your project relies on jQuery, you can use the jq method to set it.

Both methods can be implemented in jq

$(&#39;title&#39;).html(&#39;需要设置的值&#39;)
$(&#39;title&#39;).text(&#39;需要设置的值&#39;)
Copy after login

Example

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>hello</title>
		<script src="js/jquery-1.7.2.min.js"></script>
	</head>
	<body>
		<script type="text/javascript">
			// $(&#39;title&#39;).html(&#39;php中文网&#39;);
			$(&#39;title&#39;).text(&#39;php中文网&#39;);
		</script>
	</body>
</html>
Copy after login

How to modify the title of html in js

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to modify the title of html in js. 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