如何通过jQuery更改页面标题?

我想动态更改页面标题。 我的页面中有很多AJAX请求。 在每种类型的响应中,我想使用标题通知它。

那么,如何通过jQuery更改页面标题?

 document.title = "newtitle" 

据我所知,这是唯一有效的方法。 操纵

 $("title") 

IE8上会失败

标题标签和document.title之间存在细微差别,浏览器会以不同方式对待它们。

为什么jQuery这么小的任务? 使用vanilla javascript:

 document.title = "My new title"; 

更多信息:

如果您仍想使用jQuery,您只需执行以下操作:

 $("title").html("My new title"); 
 $('title').html('newTitle') 

在纯JavaScript中:

 document.title = "Insert title here"; 

在更改文档之前,应该完全加载文档。

参考:Mozilla Developer Central的Document.Title

  

另请查看http://hancic.info/change-page-title-with-jquery

 $(document).attr("title", "New Title"); 

假设你正在使用最新的jQuery,做一些简单的事情:

 $('title').text('My new title'); 

应该管用。 至少,这可以在google Chrome中进行简单的页内javascript控制台测试。 您可以使用.html而不是.text,但通常您不希望标题标记中包含HTML,因为通常不允许使用HTML并且可能显示奇怪 – 使用.text至少您知道您的新标题字符串将被转义而不是引导任何奇怪的行为。

否则我希望使用直接javascript做一些事情就好了,例如:

 document.title = 'A new title';