0

I have an array of pages

var $pages = ["events", "template", "privacy", "terms", "mentor", "party", "getinvolved", "tools"];

What I want to do is

  if ( $body[0].id !== anyOfThePageInTheArray ) {
    //do something here
    });

How can I do like if the page that they are on is not one of the page that is in the array.

I have tried .inArray and .each but I think maybe I'm doing it wrong or something.

4

3 回答 3

2

Try:

if($pages.indexOf($body[0].id) == -1){
    //code here
}
于 2013-06-13T19:43:03.080 回答
2

The native Array.indexOf works fine:

if ($pages.indexOf($body[0].id) === -1) {
    // It's not in there
}

But if you insist on jQuery:

if ($.inArray($body[0].id, $pages) === -1) {
    // It's not in there
}
于 2013-06-13T19:43:32.713 回答
2

You should use

if($.inArray($body[0].id,$pages) === -1)

When the second argument isn't in the array (first argument), it will return -1. Otherwise, it returns the index of the element.

By the way, $body[0].id returns e.g. "events", or is a number?

于 2013-06-13T19:45:26.137 回答