2013-12-14 44 views
0

我想知道是否有可能將2個if語句與else語句組合在一起。其中一個if - else是> 99天,另一個是> ie 9.這裏是2個語句。組合語句Togeher

> 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>'); 
} 
}); 

} 

>即9碼

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>'); 
     } 
    }); 

} 
+0

我可以看到正確的或者您在「僅使用兩個相同的內容,如果(即> 9)「部分..即可以是任何和相同的代碼執行? – Hardy

+0

爲什麼你甚至需要不同的版本?所有我看到的是一個類的差異,可以用CSS來管理...或者只是用jQuery removeClass去掉這個類。 – charlietfl

+0

如果我沒有誤認爲低於9的IE版本,我需要不同版本的原因不明白.html而不是.html你使用+。如果我錯了,請告訴我,這只是從互聯網上閱讀而已。 – user1236784

回答

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