簡體   English   中英

使用jQuery這個詞出現和消失?

[英]Using jQuery the word appears and disappears?

在jQuery中,我有問題。 welcome詞將出現10秒鍾。

10秒鍾后,這應該消失,另一個字bye應該再出現10秒鍾,這應該像循環過程一樣繼續進行。

誰能幫我解決這個問題????

假設文本在div#text

function changeText() {
    if ($("div#text").text() == 'welcome') {
        $("div#text").text('bye');
    } else {
        $("div#text").text('welcome');
    }
}

setInterval(changeText, 10000);

這不包括任何類型的動畫,僅更改文本。

您也可以嘗試一下:( fadein(),fadeOut())

$(function() {
        setInterval('Welcome()', 1000);
    })

    function Welcome() {        
        $('#msg').fadeIn(2000, function() {
            $('#msg').html("Welcome").fadeOut(2000, function() {
                $('#msg').fadeIn(2000).html("bye")
            })
        });
    }
function changeText() {
    var _text = $("#text");

    _text.fadeOut(500, function () {
    if (_text.text() == 'welcome') {
        _text.text('bye');
    } else {
        _text.text('welcome');
    }
        _text.fadeIn(500);
    });

}
changeText();
setInterval(changeText, 10000);

看看這個小提琴:

http://jsfiddle.net/s2rdD/

由於缺乏細節(並且有一點作業的味道),我只提供非常一般的建議。

您需要在一個時間間隔后做一些事情。 jQuery沒有內置的功能,但是沒有它就足夠簡單了: https : //developer.mozilla.org/en/DOM/window.setTimeout

聽起來您需要更新文本,jQuery可以做到這一點: http : //api.jquery.com/text/

即使在使用jQuery時也必須精簡Javascript。 一本好書可以為您提供幫助,與此類似。 http://www.amazon.com/JavaScript-Definitive-Guide-Activate-Guides/dp/0596805527/ref=pd_sim_b_2

奇怪的是,在我編寫這些答案時發布的兩個答案具有完全相同的代碼,包括格式和命名。 因此,它們都包含一個錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM