【发布时间】:2017-02-28 07:05:40
【问题描述】:
我正在尝试使用“数据库”在单击按钮时随机循环浏览一些引号,并以相同的方式将 HTML 中的现有引用更改为随机引用和现有作者
var myDatabase = [
{quote:"I have learned silence from the talkative...", author:"Khalil Gibran"},
{quote: "One of the blessings of old friends that you can afford to be stupid with them." author:"Ralph Waldo Emmerson"},
{quote: "Once we accept our limits, we go beyond them." author:"Albert Einstein"}
];
var newQuote = document.getElementById('displayedQuote');
var newAuthor = document.getElementById('author');
$("#quoteButton").click(function() {
var rand = Math.floor(Math.random()*database.length)
newQuote = myDatabase.[rand].quote;
newAuthor = myDatabase.[rand].author;
});
【问题讨论】:
-
您有问题吗?
-
我们能得到更多关于这个的信息吗?
-
不确定这是否是您的问题,但请不要使用方括号从数组中按索引获取项目,无需使用点分隔符 - 它应该是
myDatabase[rand].quote,不是myDatabase.[rand].quote。
标签: javascript jquery html multidimensional-array