【发布时间】:2014-02-26 11:34:58
【问题描述】:
我在 JS 文件中编写 swtich javascript switch 语句,发现问题一整天仍然找不到解决方案。
这是我用 jQuery 编写的 javascript 文件:
var percent = 20;
var widthbytes;
switch(percent)
{
case 0:
widthbytes=0;
break;
case (percent > 10 && percent < 20):
widthbytes=16;
break;
case (percent >=20 && percent < 30):
widthbytes=30;
break;
default:
widthbytes=0;
break;
}
average.width(widthbytes);
它总是返回默认值而不是 30。我的代码有什么问题吗?
【问题讨论】:
-
这行不通。请改用简单的 if-else 表达式。
-
你不能这样写。使用
if/else或reversed switch,虽然我不推荐后者。
标签: javascript switch-statement