我已經寫了一個函數來放置「,」和「和」之間的三個鏈接 我怎麼能減少if else語句。 在JavaScript中,我得到的計數,如果計數不爲零意味着鏈接必須以其他方式表現出來768,16被隱藏我怎麼能減少,如果其他語句
在以下情況下
function inst_grammer()
{
var otherCount = parseInt($('.global_other_count').html());
var initCount = parseInt($('.global_init_count').html());
var signCount = parseInt($('.global_sign_count').html());
var init_class = $('.inst_init');
var sign_class = $('.inst_sign');
if (signCount != 0 && initCount != 0 && otherCount == 0)
{
init_class.html('').fadeOut();
sign_class.html(' and ').fadeIn();
} else if (signCount == 0 && initCount != 0 && otherCount != 0)
{
init_class.html(' and ').fadeIn();
sign_class.html('');
} else if (signCount != 0 && initCount != 0 && otherCount != 0)
{
init_class.html(' and ').fadeIn();
sign_class.html(' , ').fadeIn();
}
else if (signCount != 0 && initCount == 0 && otherCount == 0)
{
init_class.html('').fadeOut();
sign_class.html('').fadeOut();
}
else if (signCount == 0 && initCount != 0 && otherCount == 0)
{
init_class.html('').fadeOut();
sign_class.html('').fadeOut();
}
else if (signCount == 0 && initCount == 0 && otherCount != 0)
{
init_class.html('').fadeOut();
sign_class.html('').fadeOut();
}
else if (signCount != 0 && initCount == 0 && otherCount != 0)
{
init_class.html('').fadeOut();
sign_class.html(' and ').fadeIn();
}
}
使用switch語句。 –
@MahmoodRehman也許你應該提供一個如何打開3個變量組合的例子? – Ryan
將它們放在一個數組中並使用一個循環...這只是一個簡單的問題,在所選項目之間添加「和」並淡出具有0值的元素。 –