سؤال

If there is an image on the document that has a class of "something" and the down arrow (key 40) is pressed then run function .setNextPage.

Here is the code I have so far: (the down arrow to trigger the function works, just need to add in the need for the second condition to be met)

$(document).keydown(function(e){
if (e.keyCode == 40) { 
   setNextPage('720/GIJoe/main.txt', 'ele1');
   return false;
} });

Thanks for any help!

هل كانت مفيدة؟

المحلول

$(document).keydown(function(e){
    if (e.keyCode == 40 && $('img.something').length > 0) { 
       setNextPage('720/GIJoe/main.txt', 'ele1');
       return false;
    } 
});

نصائح أخرى

if (e.keyCode == 40 && $('.something').length > 0) { 

You can check if theres an element existing in the DOM by using the native javascript .length function.

if(e.keyCode == 40 && $('img.something').length) {
     //code
}

if there isn't an img element with the class of something, it will return false and the statement won't run.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top