2012-04-01 61 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