Javascript 检查数字是否在两个数字之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28087842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if number is between two numbers
提问by fightstarr20
I have a variable called 'age' and am trying to work out if it is between two numbers. In this case I want to find out if the age is between 30 and 80.
我有一个名为“年龄”的变量,我正在尝试计算它是否在两个数字之间。在这种情况下,我想知道年龄是否在 30 到 80 之间。
I have this so far...
到目前为止我有这个...
if ( age >= 30 && age <=80 ) {
$('.display').each(function() {
$(this).css('display', '');
});
}
This works great if the age is below 30 then the .display div does not get displayed, but the last part where it checks if the number is less than 80 does not work.
如果年龄低于 30,这很有效,那么 .display div 不会显示,但检查数字是否小于 80 的最后部分不起作用。
Can anyone point me in the direction of a similar function I can read up on? Or am I doing something obvious wrong?
任何人都可以指出我可以阅读的类似功能的方向吗?还是我做错了什么?
回答by Yukulélé
This function check if number nis between aand b
此函数检查数字n是否介于a和b
function isBetween(n, a, b) {
return (n - a) * (n - b) <= 0
}
回答by Ja?ck
The condition that you wrote to determine whether a value is in between two values is fine. What seems to be happening here is that once the condition of age >= 30is reached, e.g. 31, the display attribute gets updated, but once it goes over 80, it doesn't revert back any style changes.
您编写的用于确定值是否介于两个值之间的条件是可以的。这里似乎发生的是,一旦age >= 30达到条件,例如31,显示属性得到更新,但一旦超过80,它不会恢复任何样式更改。
Instead of modifying the displayCSS property, in jQuery you would typically use the show and hide methods, such as .toggle():
display在 jQuery 中,您通常会使用 show 和 hide 方法,而不是修改CSS 属性,例如.toggle():
// only show if age is between 30 and 80
$('.display').toggle(age >= 30 && age <=80);
回答by Rory McCrossan
''isn't a correct setting for the displayproperty. From what you describe, you want to use block. Also, you don't need to use an each()loop here. Try this:
''不是该display属性的正确设置。根据您的描述,您想使用block. 此外,您不需要each()在这里使用循环。尝试这个:
if (age >= 30 && age <= 80) {
$('.display').css('display', 'block');
}
回答by Luis Rosety
As I said in this postyou can use a function similar to this one:
function between(n, a, b)
{
return ((a==n)&&(b==n))? true : ($n-$a)*($n-$b)<0;
}
And it is not limited to integer numbers as other functions are.
并且它不像其他函数那样限于整数。
回答by Niet the Dark Absol
jQuery is weird.
jQuery 很奇怪。
In plain JavaScript:
在纯 JavaScript 中:
this.style.display = "";
... will delete any displaysetting that has been made in JavaScript (or jQuery, or in raw HTML style="..."attribute), allowing the CSS-defined value (or default) to take over.
... 将删除display在 JavaScript(或 jQuery,或原始 HTMLstyle="..."属性)中所做的任何设置,允许 CSS 定义的值(或默认值)接管。
jQuery needs some kind of hack that I don't even remember right now to make it work... Not worth it IMO, when the JS is so simple.
jQuery 需要某种我现在什至不记得的 hack 才能使它工作...... IMO 不值得,当 JS 如此简单时。

