2017-01-23 125 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

相关问题