简体   繁体   English

随机文本来自jQuery数组

[英]Random Text From jQuery Array

I am trying to make an array of strings and then pluck a random string and place it in a div with class "quote". 我正在尝试创建一个字符串数组,然后选择一个随机字符串并将其放在带有“引用”类的div中。 Below is my current code. 以下是我目前的代码。

$(document).ready(function() {

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

});

What am I doing incorrectly? 我做错了什么?

Thanks 谢谢

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

try this 尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM