It is necessary that when you hover on a specific block, the handler for clicking on the down and up arrows works, but I don’t even understand how to implement it.

Here is an example.

$(document).ready(function() { $('div.test').on('mouseover', function() { $('div.test').html('Навели курсор на блок'); $('div.test').bind('keydown', function(e) { switch(e.keyCode) { case 38: $('div.test').html('Стрелка вверх'); break; case 40: $('div.test').html('Стрелка вниз'); break; } }); }); }); 
  • I picked it up a little and it turned out to be implemented in this way (although I don’t know how correct it is) Working example - Ilya

1 answer 1

The fact is that in js there is no such event that when you hover on a specific object, a keystroke test will work. If it is not an input, textarea or div with the attribute contenteditable = true.

If you need a key event to work when you hover on an object, then so:

 $('.test').mouseover(function() { $('.test').html('Навели курсор на блок'); document.onkeydown = function(event) { if(event.keyCode == 38) {$('.test').html('Стрелка вверх');} if(event.keyCode == 40) {$('.test').html('Стрелка вниз');} }; }); $('.test').mouseleave(function() { $('.test').html('Убрали курсор с блока'); document.onkeydown = null; }); 
  • Thank you very much) - Ilya