简体   繁体   中英

Add active class on keydown event

Hi I am new to stack overflow and jquery and this is my first question.

I would like to add and remove a active class to multiple divs with the same class on an keydown event and trigger a click function to active class.

the html code is something like this

 <div id="playbar_wrapper">
    <div id="standard_buttons" tabindex='0'>
    <div class="button_wrapper"><div onclick= "rwdButton()" id="prev"  class="button"><img src="prev.png"  alt="Previous Item"/></div></div>
    <div class="button_wrapper"><div onclick= "stopButton()" id="stop"  class="button"><img src="stop.png"  alt="Stop"/></div></div>
    <div class="button_wrapper active"><div onclick= "playButton()" id="play"  class="button"><img src="play.png"  alt="Play"/></div></div>
    <div class="button_wrapper"><div onclick= "pauseButton()" id="pause" class="button"><img src="pause.png" alt="Pause"/></div></div>
   <div class="button_wrapper"><div onclick= "fwdButton()" id="next"  class="button"><img src="next.png"  alt="Next Item"/></div></div>                 
</div>
</div>

And javascipt code is as follows: the keypress function:

KeypressFunctions[KeyEvent.VK_LEFT] = nextButton; 

function nextButton(){
  var currentArticle = 
$('.active').removeClass('active').next('.button_wrapper');

currentArticle= currentArticle.length > 0 ?  currentArticle : 
$('#standard_buttons div:eq(0)');
     currentArticle.addClass('active').children('.button').click(); 

}       

The click function works on the two divs at the same time. Sorry for not being clear enough. But any help would be really great.

Thanks in advance :)

A bit confuse... maybe you want something arround this?

$( "#standard_buttons div:eq(0)" ).keydown(function() {
  $( "#standard_buttons div:eq(0)").toggleClass("active");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM