javascript 比较运算符 - 大于或等于 - 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17970578/
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
Comparison Operators - Greater than or Equal to - Not Working
提问by user2638054
Just started using JQuery recently. (Fairly recently, I suppose...) What am I doing wrong here?
最近刚开始使用JQuery。(最近,我想......)我在这里做错了什么?
var userDate = new Date();
if(userDate.getHours() => 12)
{
var post = $('p[title="test"]');
post.text('Would you look at the time?');
}
回答by user1491600
The condition is flipped.
条件翻转。
var userDate = new Date();
if(userDate.getHours() >= 12)
{
var post = $('p[title="test"]');
post.text('Would you look at the time?');
}
回答by Adil
You probably need to use >=
instead of =>
, wrong sequence of greater or equal operator. The equal to = comes after > then in >=
comparison operator. You can read more about comparion operators here.
您可能需要使用>=
而不是=>
,更大或等于运算符的错误序列。等于 = 在 > 之后,然后在>=
比较运算符中。您可以在此处阅读有关比较运算符的更多信息。
var userDate = new Date();
if(userDate.getHours() >= 12)
{
var post = $('p[title="test"]');
post.text('Would you look at the time?');
}
Greater than or equal
>=
Returns true if the left operand is greater than or equal to the right operand, reference.
大于或等于
>=
返回true,如果左操作数大于或等于右操作数,参考。
回答by davy
if(userDate.getHours() => 12) {...}
Should be
应该
if(userDate.getHours() >= 12) {...}
The wrong way round maybe?
也许是错误的方式?
回答by iConnor
I must admit sometimes I get confused with the less than or equal toor greater than or equal to.
我必须承认,有时我会与小于或等于或大于或等于.
less than or equal to is
小于或等于是
<=
greater than or equal to is
大于或等于是
>=
But, if you can't remember these and your code isn't working it's always worth to simplify it buy doing something like this.
但是,如果您不记得这些并且您的代码不起作用,那么简化它总是值得购买做这样的事情。
var userDate = new Date();
if(userDate.getHours() > 11){
var post = $('p[title="test"]');
post.text('Would you look at the time?');
}
This means
这意味着
Greater than eleven
大于十一
This is the same but I think it's easier to understand, it must be 12 or greater
这是一样的,但我认为它更容易理解,它必须是12 或更大