生成随机数Jquery [英] Generate random numbers Jquery

查看:107
本文介绍了生成随机数Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在列表中显示一个随机数字。我从后端抓取列表,我只是使用这些数字作为示例。这是我的代码,我不太清楚它为什么不填写文本。

I'd like to show a random numbers within my list. Im grabbing the list from the backend, and im just using these numbers as examples. This is my code, I'm not quite sure why its not filling in the text.

setInterval(function() {
    var number = 1 + Math.floor(Math.random() * 6);
    $('.num-gen').text(number);
},

制作 在这里摆弄 向您展示我正在使用的内容。提前致谢。

Made a fiddle here to show you what I'm working with. Thanks in advance.

推荐答案

你的代码出错了,你错过了完成,1000 / *时间以ms * /为单位;

You had an error in your code, you were missing the finishing , 1000/*time in ms*/);!

此外,这里不需要jQuery,你可以在这样的Javascript中执行此操作。

Also, jQuery is not required here, you may do this in Javascript like this.

setInterval(function() {
    var number = 1 + Math.floor(Math.random() * 6);
    document.getElementsByClass('num-gen')[0].innerHtml = number;
}, 1000);

编辑:

setInterval(function() {
    jQuery.each(jQuery('.num-gen'),function(){
         var number = 1 + Math.floor(Math.random() * 6);
         jQuery(this).text(number);  
    });
}, 1000);

这篇关于生成随机数Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆