随机使用JQuery显示一个div类 [英] randomly display a div class using JQuery
本文介绍了随机使用JQuery显示一个div类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< div class =item> One< / div>
< div class =item> Two< / div>
< div class =item> Three< / div>
< div class =item> Four< / div>因此,如果随机方法(随机结果在1到4之间)给出3作为结果,那么它显示: < div class =item> Three< / div>
其余的都是隐藏的。当然,代码可以改变,使其工作。谢谢。
解决方案首先我们得到一个随机的int,从零到项目数(减1)。
然后我们隐藏所有其他元素,并显示随机选择的元素。
var random = Math.floor(Math .random()* $('。item')。length);
$('。item')。hide()。eq(random).show();
I want to randomly display a div class using JQuery. The code is something like:
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three</div>
<div class="item">Four</div>
So if the random method(random result is within 1 to 4) gives 3 as the result, it shows:
<div class="item">Three</div>
The rest are hidden. Of course, the code can be changed to make it worked. Thanks.
解决方案 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() * $('.item').length);
$('.item').hide().eq(random).show();
这篇关于随机使用JQuery显示一个div类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文