I'm looking for a solution to show only 1 div with rate 1/100. Now I'm using this javascript:

我正在寻找一个解决方案,只显示1个div,速率为1/100。现在我正在使用这个javascript:

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

It solves my problem if I create 100 div's with .item class but it makes a huge mess in my code.

它解决了我的问题,如果我用.item类创建100个div,但它在我的代码中造成了很大的混乱。

2 个解决方案

#1


0

I asume you want to show a div randomly when you refresh the page.

我想你在刷新页面时想要随机显示一个div。

Javascrip (jquery):

$(document).ready(function(e) {

    var randomNum = Math.floor(Math.random() * 100) + 1 ;
    $(".item_" + randomNum).show();

});

HTML:

<div class="item_1" style="display:none;">This div show randomly</div>

https://jsfiddle.net/12f9hpny/

When random number is 1 this div will show. Hope this will help.

当随机数为1时,此div将显示。希望这会有所帮助。

更多相关文章

  1. 等待执行所有ajax回调的最佳解决方案
  2. 【问题解决方案】ImportError: No module named 'pygal'
  3. Python 黏包及黏包解决方案
  4. 无法安装ndg-httpsclient或者我的解决方案错误
  5. Linux无法连接网络解决方案
  6. Media-S 简介(一个开源的DRM解决方案)
  7. AppScan安全问题解决方案
  8. PLSQL乱码解决方案
  9. 数据库不支持中文解决方案(mysql)

随机推荐

  1. Linux 运维自动化之Cobbler实战案例
  2. 添用户报错:useradd:警告:此主目录已经存在
  3. linux+nginx+php websocket server问题
  4. 如何使用“tail -1 * / filename”拖尾多
  5. 认识并编写一个Makefile
  6. uinx 信号 SIGINT SIGTERM SIGKILL区别
  7. Linux文件归档和解压缩
  8. 本人新手,询问一下有谁用过lameditor-1.0?
  9. Linux系统下查看目录大小
  10. ubuntu 14.04 安装 minidwep-gtk, aircra