这是我想要做的......
目前我已将代码设置为:
以下是代码http://codepen.io/fun/pen/aNedGQ?editors=1010
的链接帮助将不胜感激! =)
以下是代码,与上面的链接相同。
HTML
<div id ='display'> </div>
<div class ='button'>Next item</div>
JS
var items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// Randomly on click display each item
// Only display each item once
// Stop when all items have been displayed
// Display to screen
var display = function(info) {
var x = document.getElementById('display');
x.innerHTML = info;
}
// Create random number
var random = Math.floor(Math.random() * items.length);
// Click handler
$('.button').on('click', function() {
// Display random arr items to screen
display(items[random]);
// Next item to be random
random = (random + Math.floor(Math.random() * items.length)) % items.length;
});
答案 0 :(得分:2)
您可以显示随机项,然后将其从数组中删除
var items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$('.button').click(function() {
var rand = items[Math.floor(Math.random() * items.length)];
$('#display').text(rand);
items.splice(items.indexOf(rand), 1);
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='button'>Next item</div>
<div id='display'></div>
&#13;