title
is a special node element inhtml
. Because it can usedocument.getElementsByTagName("title")[0]
to get the content of the web pagetitle
tag, but its value cannot be changed usingdocument.getElementsByTagName("title")[0].innerHtml
. After testing, there are two ways to modify native js, and it can also be easily set in jQuery. Friends who are not sure can find out.
Throughconsole.log(document.getElementsByTagName("title")[0])
, we found that
TextNode, so I set the value of title using
innerTextmethod, and it succeeded.
document.getElementsByTagName("title")[0].innerText = '需要设置的值';
After testing, the value of title can also be set throughdocument.title.
console.log(document.title); # 可以获取title的值。 document.title = '需要设置的值'; # 设置title的值。
window.onfocus = function () { document.title = '恢复正常了...'; }; window.onblur = function () { document.title = '快回来~页面崩溃了'; };
titlehas changed.
$('title').html('') $('title').text('')
In native js, we can dynamically modify thetitle
of the web page in two ways:
innerTextand
document.titleIn .
jq, we can modify it through
$('title').html('')or
$('title').text('').
titleis a special node element in
html. Because it can use
document.getElementsByTagName ("title")[0]to get the
titletag of the web page, but cannot use
document.getElementsByTagName("title")[0].innerHtmlto change it value. After testing, there are two ways to modify native js, and it can also be easily set in jQuery. Friends who are not sure can find out.
console.log(document.getElementsByTagName("title")[0]), we found that
TextNode
, so I set the value of title usinginnerText
method, and it succeeded.document.getElementsByTagName("title")[0].innerText = '需要设置的值';
Copy after loginCopy after login
document.title method
After testing, the value of title can also be set through
document.title.
console.log(document.title); # 可以获取title的值。 document.title = '需要设置的值'; # 设置title的值。
window.onfocus = function () { document.title = '恢复正常了...'; }; window.onblur = function () { document.title = '快回来~页面崩溃了'; };
We change the value of title when the browser gains focus and loses focus. You can find that when switching browser tabs,
titlehas changed.jQuery method
$('title').html('') $('title').text('')
Both methods in jq can be implemented.
The above content is a tutorial on how to dynamically modify the browser title with js. I hope it can help everyone.
Related recommendations:
js for php development to modify the page css style Introduction to the use of js to modify the attributes of the prototype_javascript skills How to use js to modify the client registry_javascript skillsThe above is the detailed content of js dynamically modify browser title. For more information, please follow other related articles on the PHP Chinese website!