Method: 1. Use the "document.getElementsByTagName("title")[0].innerText='value'" statement; 2. Use the "document.title='value'" statement; 3. Use " $('title').html('value')" statement.
The operating environment of this tutorial: windows7 system, javascript1.8.5&&jquery1.10.2 version, Dell G3 computer.
js sets the html title title
innerText method
Through console.log(document. getElementsByTagName("title")[0]), and found that the
document.getElementsByTagName("title")[0].innerText = '需要设置的值';
document.title method
After testing, the value of title can also be set through document.title.
console.log(document.title); # 可以获取title的值。 document.title = '需要设置的值'; # 设置title的值。
jQuery method
jQuery method We change the value of title when the browser gains focus and loses focus. You can find that when switching browser tabs, The title has changed.
Of course, if your project relies on jQuery, you can use the jq method to set it
$('title').html('需要设置的值') $('title').text('需要设置的值')
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"> // $('title').html('php中文网'); $('title').text('php中文网'); </script> </body> </html>
Summary
In native js, we can dynamically modify the title of the web page in two ways: innerText
and document.title
.
jq, we can modify it through $('title').html('')
or $('title').text('')
.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to set html title in js. For more information, please follow other related articles on the PHP Chinese website!