简体   繁体   English

Javascript / JQuery-隐藏5个Div,然后显示3个随机Div

[英]Javascript / JQuery - Hide 5 Divs, then Show 3 Random Divs

I have 5 divs with the class 'testimonialBox'. 我有5个div类别为“ testimonialBox”的div。 Each div has different content. 每个div都有不同的内容。 I want all 5 to start out hidden. 我希望所有5个人都开始隐藏。

Then, I want to have 3 of those divs with that class randomly chosen and shown. 然后,我想在其中的3个div中随机选择并显示该类。 They must be 3 non-repeating, unique divs (that is, the page may show the first, third, and fourth divs, but not the first, first, and fourth divs). 它们必须是3个非重复的唯一div(也就是说,页面可能显示第一,第三和第四div,但不能显示第一,第一和第四div)。

Here is the code I am currently trying to use: 这是我当前尝试使用的代码:

var testimonials = $("div.testimonialBox");
  $(testimonials).hide();

var divs = $("div.testimonialBox").get().sort(function(){ 
    return Math.round(Math.random())-0.5;
}).slice(0,3);
  $(divs).show();

How do I do this? 我该怎么做呢?

这很奇怪,但是对我来说工作正常,而无需重复div http://jsfiddle.net/#&togetherjs=ETkPlK9C1U

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

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