简体   繁体   中英

Will this click the link when the keys are pressed?

Okay, so I need to create the code to ensure that when certain keys are pressed triggers the event as if a button were clicked. following the link through that button when they keyboard keys are pressed.

el.on('keypress', function(event){
    $('li').find('li.next');
        if (keycode == 39) {
            $('.next').click();
        }

});
el.on('keypress', function(event){
   $('li').find('li.prev');
        if (keycode == 37){
            $('.prev').click();

    }
});

You might want to have a look at the Hotkeys library for binding key events

https://github.com/tzuryby/jquery.hotkeys

JS

el.on('keyup', function (event) {
var $paginationList = el.find('ul.pagination');
const LeftKeyCode = 37;
const RightKeyCode = 39;
var keyCode = event.keyCode || event.which;
if (keyCode == LeftKeyCode){
    $paginationList.find('li.prev').find('a').click();
}
if (keyCode == RightKeyCode){
    $paginationList.find('li.next').find('a').click();
}

HTML

<li class="prev">
           <a href="<?= $pageUrl ?>" class="widget-modal-drilldown"> Previous</a>
        </li>
<li class="next">
            <a href="<?= $pageUrl ?>" class="widget-modal-drilldown">Next</a>
        </li>

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