设置页面标题的方法:1、使用“document.getElementsByTagName("title")[0].innerText='需要设置的值';”语句;2、使用“document.title = '需要设置的值';”语句。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
title在html中属于特殊的节点元素
因为它可以使用document.getElementsByTagName("title")[0]来获取网页的title标签,但却无法用document.getElementsByTagName("title")[0].innerHtml来更改它的值。
经测试原生js有两种方式可以修改,下面来介绍一下。
JavaScript设置页面标题的方法
1、innerText 方式
通过console.log(document.getElementsByTagName("title")[0]),发现能打印出
document.getElementsByTagName("title")[0].innerText = '需要设置的值';
2、document.title方式
console.log(document.title); # 可以获取title的值。document.title = '需要设置的值'; # 设置title的值。
例子
window.onfocus = function () { document.title = '恢复正常了...';};window.onblur = function () { document.title = '快回来~页面崩溃了';};
以上就是JavaScript怎么设置页面标题的详细内容。