-1

Just started using JQuery recently. (Fairly recently, I suppose...) What am I doing wrong here?

var userDate = new Date();
if(userDate.getHours() => 12)
{
    var post = $('p[title="test"]');
    post.text('Would you look at the time?');
}
4

4 回答 4

6

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?');
    }
于 2013-07-31T12:38:50.460 回答
5

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.

于 2013-07-31T12:36:49.820 回答
3
 if(userDate.getHours() => 12) {...}

Should be

 if(userDate.getHours() >= 12) {...}

The wrong way round maybe?

于 2013-07-31T12:37:29.857 回答
3

I must admit sometimes I get confused with the less than or equal to or 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

于 2013-07-31T12:44:01.930 回答