最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery blinking title - Stack Overflow

programmeradmin1浏览0评论

So, i need to have a blinking title, this code should work, but for some reason it doesn't.

Now console keeps showing me correct title, but title in my browsers doesn't change, what could be wrong?

var blink = true;

setInterval(function(){
    if(blink){
        $("title").text("test");
        blink = false;
        console.log($("title"));
    }else{
        $("title").text("");
        blink = true;
        console.log($("title"));
    }
}, 1000);

So, i need to have a blinking title, this code should work, but for some reason it doesn't.

Now console keeps showing me correct title, but title in my browsers doesn't change, what could be wrong?

var blink = true;

setInterval(function(){
    if(blink){
        $("title").text("test");
        blink = false;
        console.log($("title"));
    }else{
        $("title").text("");
        blink = true;
        console.log($("title"));
    }
}, 1000);
Share Improve this question asked Mar 22, 2012 at 21:32 LinasLinas 4,40819 gold badges74 silver badges119 bronze badges 2
  • "#title" refers to id="title" for example. "title" may not refer to document.title. Can you show us the html definition of the <title> tag? – Travis J Commented Mar 22, 2012 at 21:38
  • The code works for me as-is in FF11, of course an empty <title> just shows the URL instead, might want to try something like <tons of spaces><dot> – No Results Found Commented Mar 22, 2012 at 21:39
Add a ment  | 

2 Answers 2

Reset to default 6

Use document.title = ... <---

You are just editing an attribute which does nothing.


Try this:

setInterval(function(){
    var title = document.title;
    document.title = (title == "test" ? "none" : "test");
}, 1000);

See the title in this demo change from test to none every second. (full fiddle)

Use a direct reference:

var blink = true;

setInterval(function(){
var theTitle = document.getElementsByTagName("title")[0];
if(blink){
    theTitle.text = "test";
    //or theTitle.innerHTML = "test";
    blink = false;
}else{
    theTitle.text = "";
    //or theTitle.innerHTML = "";
    blink = true;
}
}, 1000);
发布评论

评论列表(0)

  1. 暂无评论