将陈述结合在一起

时间:2013-12-14 00:50:09

标签: javascript jquery

我想知道是否可以将2个if语句与else语句组合在一起。一个if - else用于>那么99天,另一个是>即9.这是2个陈述。

> 99天代码

function init(elem, options) {
 elem.addClass('countdownHolder');

// Time left
 var left = Math.floor((options.timestamp - (new Date())) / 1000);
 // Number of days left
 var d = Math.floor(left / days);

// Creating the markup inside the container
 $.each(['Days', 'Hours', 'Minutes', 'Seconds'], function (i) {
 if (this == 'Days' && d > 99) {
 $('<span class="count' + this + '">').html(
 '<span class="position">\
 <span class="digit static">0</span>\
 </span>\
 <span class="position">\
 <span class="digit static">0</span>\
 </span>\
 <span class="position">\
 <span class="digit static">0</span>\
 </span>'
 ).appendTo(elem);
 }
 else {
 $('<span class="count' + this + '">').html(
 '<span class="position">\
 <span class="digit static">0</span>\
 </span>\
 <span class="position">\
 <span class="digit static">0</span>\
 </span>'
 ).appendTo(elem);
 }
 if (this != "Seconds") {
 elem.append('<span class="countDiv countDiv' + i + '"></span>');
 }
 });

}

&GT;即9 Code

var ie = (function(){

var undef,
    v = 3,
    div = document.createElement('div'),
    all = div.getElementsByTagName('i');

while (
    div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
    all[0]
);

return v > 4 ? v : undef;

}());

function init(elem, options){
    elem.addClass('countdownHolder');

    // Creating the markup inside the container
    $.each(['Days','Hours','Minutes','Seconds'],function(i){
        if (ie>9){
        $('<span class="count'+this+'">').html(
            '<span class="position">\
                <span class="digit static">0</span>\
            </span>\
            <span class="position">\
                <span class="digit static">0</span>\
            </span>'
        ).appendTo(elem);
        }else{
            $('<span class="count'+this+'">' +
            '<span class="position">' +
                '<span class="digit static">0</span>' +
            '</span>' +
            '<span class="position">' +
                '<span class="digit static">0</span>' +
            '</span>' +
        '</span>').appendTo(elem);
        }
        if(this!="Seconds"){
            elem.append('<span class="countDiv countDiv'+i+'"></span>');
        }
    });

}

1 个答案:

答案 0 :(得分:1)

switch (true) {
  case d > 99: ...; break
  case ie > 9: ...; break
  default: ... /* this is your "else" */
}