使用jQuery增加数字

increasing numbers with jQuery

本文关键字:数字 增加数 增加 jQuery 使用      更新时间:2023-09-26

是否有一个简单的jQuery方法来生成数字使用.class包含不同的数字越来越多增加几秒钟后?

示例:

before:

<p class="number_vp">56</p>
<p class="number_vp">2</p>

几秒钟后:

<p class="number_vp">58</p>
<p class="number_vp">10</p>
var $p = $('.number_vp');
setInterval(function() {
    $p.html(function(_, num) {
        return +num + Math.floor(Math.random() * 6);
    })
}, 1000);
http://jsfiddle.net/eqseX/

为所有'number_vp'元素添加1-100之间的随机数,每秒一次:

setTimeout(function() {
    $(".number_vp").each(function(index,elm) {
        var value = $(elm).html()*1;
        var newValue = value + Math.round(Math.random()*100);
        $(elm).html(newValue);
    });
}, 1000);

这段代码展示了如何为number_vp类的div项生成随机数。

$('.number_vp').each(function(){
    var randomnumber=Math.floor(Math.random()*999);
    $(this).html(randomnumber);
});

下面是一个现场演示:http://jsfiddle.net/6JSXD/1/


这是一个实现了计时器的示例,每2秒改变一次值。

setInterval(function(){
    changeNumbers();    
}, 2000);
function changeNumbers(){
    $('.number_vp').each(function(){
        var randomnumber=Math.floor(Math.random()*999);
        $(this).html(randomnumber);
    });
}
http://jsfiddle.net/6JSXD/2/

setTimeout(genRandom,10000);

function genRandom(){$(".number_vp").each(function(index,elm) {
    $(this).html(Math.round(Math.random()*100000));
    setTimeout(genRandom,10000);
});}