我需要按降序编号,但当它达到0时,数字应该变成9 ..
例如,假设num是2,顺序应该是2, 1, 9, 8, 7, 6, 5, 4, 3
var num = 2;
$(function() {
$(".one").html(num);
$(".two").html((num-1)%9);
$(".three").html((num-2)%9);
$(".four").html((num-3)%9);
$(".five").html((num-4)%9);
$(".six").html((num-5)%9);
$(".seven").html((num-6)%9);
$(".eight").html((num-7)%9);
$(".nine").html((num-8)%9);
});?
Run Code Online (Sandbox Code Playgroud)
但是在代码中有0和负数,我该如何克服呢?
谢谢您的帮助.
试试这个:
var num = 3;
$(function() {
$('span').each(function(i){
num--;
if (num == 0) {
num = 9;
}
$(this).text(num)
})
});
Run Code Online (Sandbox Code Playgroud)