此代碼通過三個數組洗牌隨機順序每當DIV點擊。我想不得不讓兩個數組「引號」和「作者」顯示相同的隨機數組順序。如果x是隨機的,我希望「Third」等於「-Third」和「First」等於「-First」或者引號[x] ==作者[x]。隨機選擇項目從兩個數組和匹配的項目責令
也有一個簡單的方法來對。就緒和功能。點擊結合,所以我不已經把完全相同的代碼在這兩個?
var colors = ["#3b609b", "#9b3b3b", "#3b9b81", "#7da5a4"];
var quotes = ["First", "Second", "Third", "Fourth"];
var authors = ["-First", "-Second", "-Third", "-Fourth"];
$(document).ready(function() {
//Variables to shuffle through "colors", "quotes" and "authors" arrays.
var rand = Math.floor(Math.random() * colors.length);
var rand2 = Math.floor(Math.random() * quotes.length);
var rand3 = Math.floor(Math.random() * authors.length);
//Display quotes/authors and change background colors.
$("body, .button, .social").css("background-color", colors[rand]);
$(".quote").html(quotes[rand2]).css("color", colors[rand]);
$(".author").html(authors[rand3]).css("color", colors[rand]);
$(".button").click(function() {
//Variables to shuffle through "colors", "quotes" and "authors" arrays.
var rand = Math.floor(Math.random() * colors.length);
var rand2 = Math.floor(Math.random() * quotes.length);
var rand3 = Math.floor(Math.random() * authors.length);
//Display quotes/authors and change background colors when div is clicked.
$("body, .button, .social").css("background-color", colors[rand]);
$(".quote").html(quotes[rand2]).css("color", colors[rand]);
$(".author").html(authors[rand3]).css("color", colors[rand]);
});
});
非常感謝!它現在很好用! – Zach