简体   繁体   English

从两个数组中选择随机项目并匹配项目顺序

[英]Select random items out of two arrays and match the items order

This code shuffles through the three arrays at random order whenever the div clicked. 每当div单击时,此代码就会以随机顺序随机排列三个数组。 I would like to have to have the two arrays "quotes" and "authors" display the same random array order. 我希望两个数组“ quotes”和“ authors”显示相同的随机数组顺序。 I would like "Third" to equal "-Third" and "First" to equal "-First" or quotes[x]==authors[x] when x is random. 我希望“第三”等于“-第三”,“第一”等于“-第一”,或者当x是随机的时,引号[x] == authors [x]。

Also is there a simple way to combine the .ready and .click function so I don't have put the exact same code in both? 还有没有一种简单的方法来组合.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]);
  });
});

You can pull out that logic into another function like so! 您可以将这种逻辑引入另一个函数中!

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);
});

Use an object which contains all content for each quote and then you simply select an object at random and all elements of that object are accessible. 使用包含每个引号的所有内容的对象,然后简单地随机选择一个对象,即可访问该对象的所有元素。 This prevents the muliple arrays and makes it a cleaner code structure and easier to maintain and update. 这样可以防止出现多个数组,并使其代码结构更简洁,并且更易于维护和更新。

Not that I have abridged your function - once you get the random object - you can then manipulate your other elements with the objects properties. 并不是说我简化了您的功能-一旦获得随机对象-您就可以使用对象属性来操纵其他元素。 I would also suggest using classes with the colors and either adding or removing the class to achieve the css color change - its cleaner to add class to color an element than to change with inline CSS. 我还建议使用带有颜色的类,然后添加或删除该类以实现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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM