使用 Jquery 更改页面标题

发布于 2024-12-01 04:07:04 字数 180 浏览 4 评论 0原文

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

示例:将 3 个 > 符号一一相加

> title
>> title
>>> title

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

example: adding 3 > symbols one by one

> title
>> title
>>> title

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

小草泠泠 2024-12-08 04:07:04
$(document).prop('title', 'test');

这只是一个 JQuery 包装器,用于:

document.title = 'test';

添加 >定期你可以这样做:

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

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

This is simply a JQuery wrapper for:

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();
等风来 2024-12-08 04:07:04

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

document.title = "blarg";

请参阅此问题了解更多详细信息。

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

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

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

var counter = 0;

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

将两者串在一起,以便在循环中单击按钮时动态更改:

var counter = 0;

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

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

document.title = "blarg";

See this question for 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);
});
甜是你 2024-12-08 04:07:04

使用

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

using

$('title').html("new title");
a√萤火虫的光℡ 2024-12-08 04:07:04
 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);
        });

 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);
        });

凯凯我们等你回来 2024-12-08 04:07:04

HTML 代码:

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

Jquery 代码:

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

Html code:

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()); 
    });
});
你好,陌生人 2024-12-08 04:07:04

我使用(并推荐):

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

它也适用于 IE
这是 Some will Debt on which is better, prop or attr 的别名

document.title = "Another Title";

,因为 prop call DOM属性和 attr 调用 HTML 属性,我认为这实际上更好......

在 DOM 加载后使用它

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

希望这会有所帮助。

i use (and recommend):

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

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

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...

use this after the DOM Load

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

hope this helps.

↘人皮目录ツ 2024-12-08 04:07:04

一些用于浏览标题列表(循环或一次性)的代码:

    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() );

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() );
帥小哥 2024-12-08 04:07:04

我用这个:

document.title = "your_customize_title";

I use this one:

document.title = "your_customize_title";
蓝咒 2024-12-08 04:07:04

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

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

这里是 Jquery 方法:

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

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

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

Here the Jquery method:

$(document).ready(function(){   
    $("#changeTitle").click(function() {
       $(document).prop('title','I am New One');
    });
});
夏末染殇 2024-12-08 04:07:04
document.title="your title";

我更喜欢这个。

document.title="your title";

I prefer this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文