2017-01-23 6 views
0

このコードは、divがクリックされるたびに3つの配列をランダムに並べ替えます。 2つの配列 "引用符"と "著者"に同じランダム配列を表示させる必要があります。私はxがランダムであるときに "第3"が "第3"と "第1"が " - 最初"または引用[x] ==著者[x]と等しいことを望みます。2つの配列からランダムな項目を選択し、項目の順序に一致させます

また、.readyと.click関数を組み合わせる簡単な方法があるので、私は両方に正確に同じコードを入れていないでしょうか?

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の色の変更を達成するためにクラスを追加または削除することをお勧めします。そんなに良く見える

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

関連する問題