将键盘绑定到Onclick事件 [英] Binding Keyboard to Onclick Event
本文介绍了将键盘绑定到Onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在进行jquery幻灯片演示时遇到问题,我想绑定下一个/向右以使图像向前/向后跳转.
I am having problems with a jquery slideshow where I want to bind next/right to make the images jump forwards/back.
我使用以下代码尝试执行此操作,但它只是刷新了页面.
I used the following code to attempt to do it but it simply refreshes the page.
<script>
$(function() {$(document).keyup(function(e) {
switch(e.keyCode) { case 37 : window.location = $('#prev a').attr('href'); break;
case 39 : window.location = $('#next a').attr('href'); break; }});});
</script>
我试图打电话给的人是:
The I was attempting to call were:
<a href='#' id='prev' onclick='show_image($prev); return false;'>
和
<a href='#' id='next' onclick='show_image($next); return false;'>
有人知道剪裁中间人并简单地将左/右绑定到每个onclick事件的简单方法吗?
Does anyone know an easy way to cut out the middle man and simply bind left/right to the onclick event for each?
任何帮助将不胜感激!
推荐答案
这应该有效:
$(function() {
$(document).keyup(function(e) {
switch (e.keyCode) {
case 37:
$('#prev a')[0].onclick();
break;
case 39:
$('#next a')[0].onclick();
break;
}
});
});
只需根据您按下的键来调用左右按钮的onclick
事件处理程序即可.
Just call the onclick
event handler of the left or right button based on which key you pressed.
这篇关于将键盘绑定到Onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文