Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按降序排序编号,不带0_Javascript_Jquery - Fatal编程技术网

Javascript 按降序排序编号,不带0

Javascript 按降序排序编号,不带0,javascript,jquery,Javascript,Jquery,我需要按降序排列数字,但当它达到0时,数字应该变成9 例如,假设num是2,顺序应该是2,1,9,8,7,6,5,4,3 但是在代码中有0和负数,我如何克服呢 谢谢您的帮助。试试这个: var num = 3; $(function() { $('span').each(function(i){ num--; if (num == 0) { num = 9; } $(this).text(num)

我需要按降序排列数字,但当它达到0时,数字应该变成9

例如,假设num是2,顺序应该是2,1,9,8,7,6,5,4,3

但是在代码中有0和负数,我如何克服呢

谢谢您的帮助。

试试这个:

var num = 3;
$(function() {
    $('span').each(function(i){
        num--;
        if (num == 0) {
           num = 9;
        }
        $(this).text(num)
    })
});
试试这个:

var num = 3;
$(function() {
    $('span').each(function(i){
        num--;
        if (num == 0) {
           num = 9;
        }
        $(this).text(num)
    })
});

不是最干净的代码,但会按照你说的做:

var num = 2;
num = num + 9;
$(function() {
    $(".one").html(num % 9 || 9);
    $(".two").html((num-1)%9 || 9);
    $(".three").html((num-2)%9 || 9);
    $(".four").html((num-3)%9 || 9);
    $(".five").html((num-4)%9 || 9);
    $(".six").html((num-5)%9 || 9);
    $(".seven").html((num-6)%9 || 9);
    $(".eight").html((num-7)%9 || 9);
    $(".nine").html((num-8)%9 || 9);
});​

不是最干净的代码,但会按照你说的做:

var num = 2;
num = num + 9;
$(function() {
    $(".one").html(num % 9 || 9);
    $(".two").html((num-1)%9 || 9);
    $(".three").html((num-2)%9 || 9);
    $(".four").html((num-3)%9 || 9);
    $(".five").html((num-4)%9 || 9);
    $(".six").html((num-5)%9 || 9);
    $(".seven").html((num-6)%9 || 9);
    $(".eight").html((num-7)%9 || 9);
    $(".nine").html((num-8)%9 || 9);
});​
清洁剂:

var num = 2;
$("span").each(function(idx) {
    $(this).text(((num - idx) % 9 + 9) % 9 || 9);
});
清洁剂:

var num = 2;
$("span").each(function(idx) {
    $(this).text(((num - idx) % 9 + 9) % 9 || 9);
});
这是小提琴:


这是一个小问题:

这不是排序,而是递减,使用带有if语句的循环检查num为0可以非常轻松地完成。这不是排序,而是递减,使用带有if语句的循环检查num为0可以非常轻松地完成。