Home  >  Article  >  Web Front-end  >  How to modify the title of html in js

How to modify the title of html in js

青灯夜游
青灯夜游Original
2021-09-03 16:53:367054browse

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 = '需要设置的值';

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>

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;)

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>

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!

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