2017-01-23 126 views
0

此代碼通過三個數組洗牌隨機順序每當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]); 
    }); 
}); 

回答

1

使用其中包含每個報價的所有內容對象,然後你只需隨意選擇一個對象,該對象的所有元素都可以訪問。這可以防止多個數組,並使其更簡潔,更易於維護和更新。

不,我已經刪減您的功能 - 一旦你的隨機對象 - 那麼你可以操縱你與對象的屬性等元素。我還建議使用具有顏色的類,並添加或刪除類以實現css顏色更改 - 其更清潔可以添加類來爲元素着色,而不是使用內聯CSS更改。

var quotes = [ 
 
    {color: "#3b609b", quote: "First", author: "-First"}, 
 
    {color: "#9b3b3b", quote: "Second", author: "-Second"}, 
 
    {color: "#3b9b81", quote: "Third", author: "-Third"}, 
 
    {color: "#7da5a4", quote: "Fourth", author: "-Fourth"} 
 
]; 
 

 

 
$(document).ready(function() { 
 
    $('#clickMe').click(function(){ 
 
    var randomQuote = quotes[Math.floor(Math.random() * quotes.length)]; 
 
    $('#color').text('Color: ' + randomQuote.color); 
 
    $('#quote').text('Quote: ' + randomQuote.quote); 
 
    $('#author').text('Author: ' + randomQuote.author); 
 
    }) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button type="button" id="clickMe">Click for a random quote</button> 
 
<p id ="color"></p> 
 
<p id ="quote"></p> 
 
<p id ="author"></p>

+0

非常感謝!它現在很好用! – Zach

1

您可以將該邏輯拉出到另一個功能,就像這樣!

var colors = ["#3b609b", "#9b3b3b", "#3b9b81", "#7da5a4"]; 
var quotes = ["First", "Second", "Third", "Fourth"]; 
var authors = ["-First", "-Second", "-Third", "-Fourth"]; 

function handle() { 
    //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]); 
} 

$(document).ready(function() { 
    handle() 
    $(".button").click(handle); 
}); 
+0

感謝,看起來好多了!它在我將其改爲.click(句柄)之後起作用。 – Zach

+0

哎呀!對於那個很抱歉!更新了片段! – spoonscen

相關問題