javascript 来自 jQuery 数组的随机文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9960909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:22:07 来源:igfitidea点击:
Random Text From jQuery Array
提问by conbask
I am trying to make an array of strings and then pluck a random string and place it in a div with class "quote". Below is my current code.
我正在尝试创建一个字符串数组,然后随机抽取一个字符串并将其放置在一个带有“quote”类的 div 中。下面是我目前的代码。
$(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
谢谢
回答by Torrent Lee
$(document).ready(function() {
var quotes = new Array("foo", "bar", "baz", "chuck"),
randno = quotes[Math.floor( Math.random() * quotes.length )];
$('.quote').text( randno );
});
try this
试试这个