randomly display a div class using JQuery

前端 未结 5 791
轻奢々
轻奢々 2020-12-08 17:28

I want to randomly display a div class using JQuery. The code is something like:

One
Two
5条回答
  •  执念已碎
    2020-12-08 17:50

    First we get a random int, from zero to the number of items (minus 1). Then we hide all other elements and show just the randomly chosen one.

    var random = Math.floor(Math.random() * jQuery('.item').length);
    jQuery('.item').hide().eq(random).show();
    

    Demo

提交回复
热议问题