如何通过函数动态更改文档标题?
How to dynamically change document title via function?
我想动态更改文档的标题。
首先我尝试了这个片段:
var milis = 3000,
titles = [
'Setting up color scheme'
], count = 0;
titles.push(document.title);
setInterval(function(){
document.title = titles[count % titles.length];
++count;
}, milis);
这非常有效,但我需要将代码片段放入一个函数中,以便我可以随时附加标题。
函数看起来像这样
function setUpTitle(){
var milis = 3000;
for(var i in arguments){
titles = [
arguments[i]
], count = 0;
titles.push(document.title);
setInterval(function(){
document.title = titles[count % titles.length];
++count;
}, milis);
}
}
setUpTitle('one', 'two', 'three', ..., document.title);
但标题根本没有改变。
我该如何更改它?
哦,你正在覆盖你的变量。试试这个:
function setUpTitle(){
var milis = 1000;
var titles = [document.title];
var count = 0;
for(var i in arguments){
titles.push(arguments[i]);
}
setInterval(function(){
document.title = titles[count % titles.length];
++count;
}, milis);
}
setUpTitle('one', 'two');
试试这个:
var currentTitle = document.title;
var beginSubmission = setInterval(function(){
loaded+=1;
bar.css('width', loaded+'%');
document.title = loaded+'% :: ' + currentTitle;
text.empty().append(loaded + "% completed. <br>");
if (loaded >= 100){
clearInterval(beginSubmission);
text.html('Done');
document.title = currentTitle;
}
}, time);
这会备份您的标题并更改其上的百分比。最后把原来的标题放回页面。
另外,删除这个:
var milis = 3000,
titles = [
'Setting up color scheme'
], count = 0;
titles.push(document.title);
title = setInterval(function(){
document.title = titles[count % titles.length];
++count;
}, milis);
或替换为:
document.title = 'Setting up color scheme';
我想动态更改文档的标题。
首先我尝试了这个片段:
var milis = 3000,
titles = [
'Setting up color scheme'
], count = 0;
titles.push(document.title);
setInterval(function(){
document.title = titles[count % titles.length];
++count;
}, milis);
这非常有效,但我需要将代码片段放入一个函数中,以便我可以随时附加标题。
函数看起来像这样
function setUpTitle(){
var milis = 3000;
for(var i in arguments){
titles = [
arguments[i]
], count = 0;
titles.push(document.title);
setInterval(function(){
document.title = titles[count % titles.length];
++count;
}, milis);
}
}
setUpTitle('one', 'two', 'three', ..., document.title);
但标题根本没有改变。
我该如何更改它?
哦,你正在覆盖你的变量。试试这个:
function setUpTitle(){
var milis = 1000;
var titles = [document.title];
var count = 0;
for(var i in arguments){
titles.push(arguments[i]);
}
setInterval(function(){
document.title = titles[count % titles.length];
++count;
}, milis);
}
setUpTitle('one', 'two');
试试这个:
var currentTitle = document.title;
var beginSubmission = setInterval(function(){
loaded+=1;
bar.css('width', loaded+'%');
document.title = loaded+'% :: ' + currentTitle;
text.empty().append(loaded + "% completed. <br>");
if (loaded >= 100){
clearInterval(beginSubmission);
text.html('Done');
document.title = currentTitle;
}
}, time);
这会备份您的标题并更改其上的百分比。最后把原来的标题放回页面。
另外,删除这个:
var milis = 3000,
titles = [
'Setting up color scheme'
], count = 0;
titles.push(document.title);
title = setInterval(function(){
document.title = titles[count % titles.length];
++count;
}, milis);
或替换为:
document.title = 'Setting up color scheme';