jquery Using ranges in switch cases?(jquery 在切换案例中使用范围?)
问题描述
开关盒通常是这样的
Monday:
Tuesday:
Wednesday:
etc.
我想使用范围.
from 1-12:
from 13-19:
from 20-21:
from 22-30:
有可能吗?顺便说一句,我正在使用 javascript/jquery.
Is it possible? I'm using javascript/jquery by the way.
推荐答案
你可以尝试滥用开关跌倒行为
you could try abusing the switch fall through behaviour
var x = 5;
switch (x) {
case 1: case 2: case 3: case 4: ...
break;
case 13: case 14: case 15: ...
break;
...
}
非常冗长
或者你可以试试这个
function checkRange(x, n, m) {
if (x >= n && x <= m) { return x; }
else { return !x; }
}
var x = 5;
switch (x) {
case checkRange(x, 1, 12):
//do something
break;
case checkRange(x, 13, 19):
...
}
这会让你得到你想要的行为.我在 checkRange 的 else 中返回 !x
的原因是为了防止将 undefined
传递到 switch 语句时出现问题.如果您的函数返回 undefined
(就像 jdk 的示例一样)并且您将 undefined
传递给开关,那么第一个案例将被执行.!x
保证在任何相等性测试下都不等于 x
,这是 switch 语句选择执行哪种情况的方式.
this gets you the behaviour you would like. The reason i return !x
in the else of checkRange is to prevent the problem of when you pass undefined
into the switch statement. if your function returns undefined
(as jdk's example does) and you pass undefined
into the switch, then the first case will be executed. !x
is guaranteed to not equal x
under any test of equality, which is how the switch statement chooses which case to execute.
这篇关于jquery 在切换案例中使用范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jquery 在切换案例中使用范围?


基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01