开发者

how to change text in a div with a link, and keep changing it every time link is clicked

开发者 https://www.devze.com 2023-03-23 23:28 出处:网络
is there a way to change the text inside a div using a link, but to keep changing it every time the link is clicked? I tried innerhtml and I can get it to change once, but after it is changed how to c

is there a way to change the text inside a div using a link, but to keep changing it every time the link is clicked? I tried innerhtml and I can get it to change once, but after it is changed how to change y开发者_运维知识库et again to new text. The end result is every time the "next" link is clicked new text loads? I do not have any code to start from so any help would be great


I'm not really sure you want the text to be, but you should be able to customize this to make it work no matter where you're getting the text from:

(function(){
    var texts = [
    'This is the first message',
    'This is the second message',
    'This is the last message'
    ], i = 0;

    document.getElementById('text').innerHTML = texts[0];

    document.getElementById('left').onclick = function(){
        if(i <= 0)
            i += 3;
        document.getElementById('text').innerHTML = texts[--i%texts.length];
    }

    document.getElementById('right').onclick = function(){
        document.getElementById('text').innerHTML = texts[++i%texts.length];
    }
})();

http://jsfiddle.net/YDA7v/

0

精彩评论

暂无评论...
验证码 换一张
取 消