【发布时间】:2014-01-15 09:38:44
【问题描述】:
我写了一个函数来在三个链接之间放置“,”和“and” 我如何减少 if else 语句。 在 javascript 中,如果计数不为零,我会得到计数,这意味着链接必须显示,否则应该隐藏
在以下场景中
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 个变量组合的示例?
-
将它们放在一个数组中并使用循环...这只是一个简单的问题,在所选项目之间附加“and”并淡出具有 0 值的元素。
-
我想减少代码,
标签: javascript php jquery