你可以一次又一次地改变网页标题

时间:2016-10-22 08:44:02

标签: javascript jquery title titlebar page-title

假设我有一个标题为#34的网页,您好我是一个标题"但是在页面加载后5秒钟就变成了#34;嗨我是另一个标题"然后5秒后它变成"我也是一个标题"然后这种情况一次又一次地发生。有人请指导我如何实现这一目标。我从w3schools.com上学了一些在线教程。我真的想这样做,但我甚至不知道从哪里开始。

6 个答案:

答案 0 :(得分:2)

var i=0;
setInterval(function(){
    var titles=['Hi everyone', 'Salut tout le monde', 'Cao svima'];//add more titles if you want
    if(i===titles.length) {
        i=0;
    }
    document.title = titles[i];
    i++;
}, 5000);

答案 1 :(得分:2)

此代码中最复杂的部分是使用modulo根据titles数组的长度获取当前迭代。

(ii++ % titles.length)

我们正在增加迭代器ii++,然后使用modulo(除法余数)来计算迭代器的当前值。这样您就可以根据需要使用多个标题。

const titles = [
  'Hi I am a title',
  'Hi I am another title',
  'I am also a title'
]

function changeTitles(titles){
  // save an iterator in a closure
  let ii = 0
  // update is run at the start
  return (function update() {
    // change the title
    document.querySelector('title').textContent = titles[(ii++ % titles.length)]
    // queue the function to be called in 5 seconds
    setTimeout(update, 5000)
  })()
}

changeTitles(titles)

答案 2 :(得分:0)

您可以使用setInterval()之类的内容,同时设置document.title

要动态更改标题,请创建一个字符串消息数组(请参阅Arrays)。在你为setInterval()编写的函数内部,每次运行时都会获得random integer。您可以使用它来索引您已经制作的字符串数组。将该值设置为document.title。

或者,这里有一些快速演示代码可以帮助您:

<html>
    <script type="text/javascript">
        messages = [ "one", "two", "three" ]

        function rotateTitle() {
            index = 0

            setInterval(function() {
                if (messages.length == index) {
                    index = 0
                }

                document.title = messages[index]

                index++
            }, 5000)
        }
    </script>
<body onLoad="rotateTitle()">
</body>
</html>

答案 3 :(得分:0)

这样的事情对你有用。

The Demo on Jsfiddle

HTML(为外观重新加载div:P)

<div id="page_title"></div>
<br>
<span id="text-reload">Reload</span>

JS

// custom jquery plugin loadText()
    $.fn.loadText = function( textArray, interval ) {
        return this.each( function() {
            var obj = $(this);
            obj.fadeOut( 'slow', function() {
                var elem = textArray[0];
                obj.empty().html( elem );
                textArray.shift();
                textArray.push(elem);
                obj.fadeIn( 'fast' );
            });
            timeOut = setTimeout( function(){ obj.loadText( textArray, interval )}, interval );
            $("#text-reload").click( function(){ 
                if( !obj.is(':animated') ) { clearTimeout( timeOut ); obj.loadText( textArray, interval );} // animation check prevents "too much recursion" error in jQuery 
            });
        });
    };

    $(document).ready(function() {
        var helloArray = ["Hi I am a title", "Hi I am another title", "I am also a title", "more", "and more", "and even more", "aloha", "see ya!"];
        $('#page_title').loadText( helloArray, 5000 ); // ( array, interval )
        document.title = $('#page_title').text();
    });

答案 4 :(得分:0)

试试这个

def test():
    fnames = glob(in_loc+'*.pdb')

    for each in fnames:
    # This is the new generated file out of input file (.txt).
        formatted_file = each.replace('pdb', 'txt')
        suffix = 'txt'

        formatted_file = formatted_file.replace(in_loc, out_loc)
        ofstem = each.replace('.pdb', '')

    # This is the input file
        in_f = open(each, 'r')

    # A new file to be opened.
        # out_f = open(formatted_file, "w")

    # Filtering results from input file
        try:
            out_chain_list = filter_file(in_f)
            for each_line in out_chain_list:
            # open and write output file for each out_chain item
                out_f = open('{}/{}{}.{}'.format(out_loc, 
                                       ofstem, 
                                       each_line, 
                                       suffix), 'a')
                out_f.write(each_line)
                out_f.close()

        # Closing all the opened files.
            in_f.close()

        except Exception as e:
            print('Exception for file: ', each, '\n', e)
            out_f.close()
            in_f.close()

答案 5 :(得分:0)

<!DOCTYPE html>
<html>
<head>
    <title>hello</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>

<script>
$(document).ready(function(){
    var titles = ['title1', 'title 2', 'title 3'];

    var timeInterval = 5000; /** interval between each titles **/

    exec();

    setInterval(function(){
        exec();
    }, timeInterval * titles.length);

    function exec(){
        $.each(titles, function(k, v){
            setTimeout(function(){
                $('title').html(v);
            }, timeInterval * (k + 1));
        });
    }

});
</script>
</body>
</html>