Javascript 使用 Jquery 更改页面标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7173596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:10:54  来源:igfitidea点击:

Changing the page title with Jquery

javascriptjquerytitle

提问by Ilya Medvedev

How to make dynamic changing <title>tag with jquery?

如何<title>使用jquery制作动态变化的标签?

example: adding 3 >symbols one by one

例子:3个>符号一一相加

> title
>> title
>>> title

回答by Gazler

$(document).prop('title', 'test');

This is simply a JQuery wrapper for:

这只是一个 JQuery 包装器:

document.title = 'test';

To add a > periodically you can do:

要定期添加 > 您可以执行以下操作:

function changeTitle() {
    var title = $(document).prop('title'); 
    if (title.indexOf('>>>') == -1) {
        setTimeout(changeTitle, 3000);  
        $(document).prop('title', '>'+title);
    }
}

changeTitle();

回答by Spycho

There's no need to use jQuery to change the title. Try:

无需使用 jQuery 来更改标题。尝试:

document.title = "blarg";

See this questionfor more details.

有关更多详细信息,请参阅此问题

To dynamically change on button click:

要在按钮单击时动态更改:

$(selectorForMyButton).click(function(){
    document.title = "blarg";
});

To dynamically change in loop, try:

要在循环中动态更改,请尝试:

var counter = 0;

var titleTimerId = setInterval(function(){
    document.title = document.title + '>';
    counter++;
    if(counter == 5){
        clearInterval(titleTimerId);
    }
}, 100);

To string the two together so that it dynamically changes on button click, in a loop:

将两者串在一起,使其在按钮点击时动态变化,在一个循环中:

var counter = 0;

$(selectorForMyButton).click(function(){
  titleTimerId = setInterval(function(){
    document.title = document.title + '>';
    counter++;
    if(counter == 5){
        clearInterval(titleTimerId);
    }
  }, 100);
});

回答by Gowri

using

使用

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

回答by Shahbaz Ahmad

 var isOldTitle = true;
        var oldTitle = document.title;
        var newTitle = "New Title";
        var interval = null;
        function changeTitle() {
            document.title = isOldTitle ? oldTitle : newTitle;
            isOldTitle = !isOldTitle;
        }
        interval = setInterval(changeTitle, 700);

        $(window).focus(function () {
            clearInterval(interval);
            $("title").text(oldTitle);
        });

回答by Sakes Yordi

i use (and recommend):

我使用(并推荐):

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

and it works in IE as well this is an alias to

它也适用于 IE 这是一个别名

document.title = "Another Title";

Some will debate on wich is better, prop or attr, and since prop call DOM properties and attr Call HTML properties, i think this is actually better...

有些人会争论到底哪个更好, prop 还是 attr,并且由于 prop 调用 DOM 属性和 attr 调用 HTML 属性,我认为这实际上更好......

use this after the DOM Load

在 DOM 加载后使用它

$(function(){
    $(document).attr("title", "Another Title");
});

hope this helps.

希望这可以帮助。

回答by etuardu

Some code to walk through a list of titles (circularily or one-shot):

一些代码来遍历标题列表(循环或一次性):

    var titles = [
            " title",
            "> title",
            ">> title",
            ">>> title"
    ];

    // option 1:
    function titleAniCircular(i) {
            // from first to last title and back again, forever
            i = (!i) ? 0 : (i*1+1) % titles.length;
            $('title').html(titles[i]);
            setTimeout(titleAniCircular, 1000, [i]);
    };

    // option 2:
    function titleAniSequence(i) {
            // from first to last title and stop
            i = (!i) ? 0 : (i*1+1);
            $('title').html(titles[i]);
            if (i<titles.length-1) setTimeout(titleAniSequence, 1000, [i]);
    };

    // then call them when you like.
    // e.g. to call one on document load, uncomment one of the rows below:

    //$(document).load( titleAniCircular() );
    //$(document).load( titleAniSequence() );

回答by Haimei

I use this one:

我用这个:

document.title = "your_customize_title";

回答by Ekta P. Sutariya

Html code:

html代码:

Change Title:
<input type="text" id="changeTitle" placeholder="Enter title tag">
<button id="changeTitle1">Click!</button>

Jquery code:

查询代码:

$(document).ready(function(){   
    $("#changeTitle1").click(function() {
        $(document).prop('title',$("#changeTitle").val()); 
    });
});

回答by Raheem

document.title="your title";

I prefer this.

我更喜欢这个。

回答by Ekta P. Sutariya

Its very simple way to change the page title with jquery..

使用jquery更改页面标题的非常简单的方法..

<a href="#" id="changeTitle">Click!</a>

Here the Jquery method:

这里的Jquery方法:

$(document).ready(function(){   
    $("#changeTitle").click(function() {
       $(document).prop('title','I am New One');
    });
});