简体   繁体   中英

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

I have 5 divs with the class 'testimonialBox'. Each div has different content. I want all 5 to start out hidden.

Then, I want to have 3 of those divs with that class randomly chosen and shown. 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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