Javascript 使用<;标题>;标记以加载多个不同的文本字符串

Javascript 使用<;标题>;标记以加载多个不同的文本字符串,javascript,html,tags,title,Javascript,Html,Tags,Title,我见过一些站点(例如),它们的标签会每隔一段时间更改,我认为这是通过Javascript完成的,但我还没有看到任何关于这种操作的文档 我不相信document.title会起作用,但也许我误解了如何正确使用它 有人见过这样的事情吗,或者怎么做才是最好的 基本上,在HTML中,每隔几秒钟就会出现这样的情况: <title>Title 1</title> 标题1 几秒钟后 <title>Title 2</title> 标题2 再过几秒钟 &l

我见过一些站点(例如),它们的标签会每隔一段时间更改,我认为这是通过Javascript完成的,但我还没有看到任何关于这种操作的文档

我不相信document.title会起作用,但也许我误解了如何正确使用它

有人见过这样的事情吗,或者怎么做才是最好的


基本上,在HTML中,每隔几秒钟就会出现这样的情况:

<title>Title 1</title>
标题1
几秒钟后

<title>Title 2</title>
标题2
再过几秒钟

<title>Title 3</title>
标题3

您应该使用window setInterval方法,然后使用选择器修改标题元素内容

来自W3school文档:
我认为
document.title
会很好用。试试这个:

var titles = ["Title1", "Title2", "Title3"];
var currentTitle = 0;
setInterval(function(){
    document.title = titles[currentTitle];
    if (currentTitle < titles.length - 1) {
        currentTitle++;
    } else {
        currentTitle = 0;
    }
}, 3000);
var titles=[“Title1”、“Title2”、“Title3”];
var currentTitle=0;
setInterval(函数(){
document.title=标题[当前标题];
如果(当前标题<标题长度-1){
currentTitle++;
}否则{
currentTitle=0;
}
}, 3000);
如果将此脚本添加到页面中,它应每隔三秒将页面标题更改为
titles
数组的下一个元素,并无限期地循环回数组的开头

要更改更改更改之间的时间量,只需将
3000
更改为您希望更改之间的毫秒数

要在任何点停止循环,可以使用
clearInterval()


这能解决您的问题吗?

除了
编写
之外,
文档中还有很多方法和属性。在这种情况下,您可以使用
title
属性访问和修改标题

通过此操作和超时/间隔,您可以循环标题,执行以下操作:

var titles = ["Title 1", "My second title", "yay! third title shown!"];
setInterval(function() {
    document.title = titles.shift(); // Get the first element in the array and remove it.
    titles.push(document.title); // Push the element to the end of the array
}, 5000); // Milliseconds to loop
我有点喜欢codegolf,所以你可以这样做:p:

setInterval(function() {
    titles.push(document.title = titles.shift()); // Get the first element in the array, remove it, assign it to title and push it back to the end of the array.
}, 5000);

setInterval(function(){document.title=“title”+Math.ceil(Math.random()*5)},1000)您的问题/接受答案比率太低(实际上为零)。如果你希望人们将来帮助你,你应该开始接受问题的答案。人们花时间帮助你是有原因的,所以请心存感激。我喜欢这种推/移的解决方案,而不是循环数组,非常酷!一个漂亮的高尔夫:D@steveg哈哈,thanx xD