I use this script to navigate with keyboard arrow keys. But the problem is that i can't trigger click.
<script>
$(document).keyup(function(e){
switch (e.keyCode){
case 39:
if( $(".imgnav-next").attr("href")){
$(".imgnav-next").trigger("click");
}
开发者_Python百科 break;
case 37:
if( $(".imgnav-prev").attr("href")){
$(".imgnav-prev").trigger("click");
}
break;
}
});
</script>
This is the html part with navigation:
<div class="imgnav-prev-container">
<a href="prev.html" class="imgnav-prev">Next pic</a>
</div>
<div class="imgnav-next-container">
<a href="next.html" class="imgnav-next">Previous pic</a>
</div>
This should work:
$(document).keyup(function(e){
switch (e.keyCode){
case 39:
if( $(".imgnav-next").attr("href")){
window.location = $(".imgnav-next").attr("href");
}
break;
case 37:
if( $(".imgnav-prev").attr("href")){
window.location = $(".imgnav-prev").attr("href");
}
break;
}
});
It's actually clicking, it just doesn't seem to do anything because you don't have anything binded to your a
tags that says it got clicked. Also I don't think it's allowed to do fake clicking in browsers using JS, I mean, like the same behavior when you literally click it with your mouse and goes to the URL. You can code to make it click
it but it won't go to the href
like when you literally click it. You can however fake the action by something like:
`window.location.href = $(".imgnav-next").attr("href");`
精彩评论