我正在构建一个随机引用生成器。我无法弄清楚如何从数组中删除已经显示的引用,因此单击该按钮时它不会显示两次。有什么建议?这是javascript:
var quoteArray = [
{
content: "quote 1 goes in here",
author: "author 1"
},
{
content: "quote 2 goes in here",
author: "author 2"
}
];
var button = document.getElementById('quote-button'),
quote = document.getElementById('quote'),
author = document.getElementById('quote-author'),
random;
window.onload = randomQuote;
button.addEventListener('click', randomQuote);
function randomQuote (){
random = Math.floor(Math.random() * quoteArray.length);
quote.innerHTML = quoteArray[random].content;
author.innerHTML = '— ' + quoteArray[random].author;
}
提前致谢!
答案 0 :(得分:0)
您可以使用Array.prototype.splice(start, count)功能。
例如:
var array = [1, 2, 3, 4, 5]
// remove 2 in the array.
var index = 1;
array.splice(1, 1);
// array = [1, 3, 4, 5]