2012-04-01 25 views
2

我想製作一個字符串數組,然後採摘一個隨機字符串,並將其放在類「報價」的div。以下是我目前的代碼。從jQuery數組隨機文本

$(document).ready(function() { 

    var quotes = new Array("foo", "bar", "baz", "chuck"); 
    var randno = Math.floor (Math.random() * quotes.length); 
    $('.quote').add(quotes[randno]); 

}); 

我在做什麼不正確?

感謝

+2

什麼是填充randno變量 – mguymon 2012-04-01 00:31:23

+0

對不起,忘了補充該行。剛添加它。 – 585connor 2012-04-01 00:33:05

+0

它是一個JavaScript數組,而不是一個jQuery數組。最好使用''[...]'而不是'new Array(...)' – ThiefMaster 2012-04-01 08:01:30

回答

10
$(document).ready(function() { 
    var quotes = new Array("foo", "bar", "baz", "chuck"), 
    randno = quotes[Math.floor(Math.random() * quotes.length)]; 
    $('.quote').text(randno); 
}); 

試試這個

+0

謝謝,效果很好。 – 585connor 2012-04-01 00:34:38

+3

哈哈在我要發佈之前從字面上看是對的。 +1類似的腦波http://jsfiddle.net/byu6V/ – 2012-04-01 00:35:06

+0

這也幫助我了。簡短而甜美。謝謝 – blackhawk 2014-07-17 22:51:33