每当我用鼠标在<div>
中向下滚动时,我成功地启用了此警报显示。
$('#divID').bind('mousewheel DOMMouseScroll', function (event) {
if (event.originalEvent.wheelDelta < 0 || event.originalEvent.detail > 0)
{
alert('scroll down with scroll');
}
});
每当我在 <div>
中使用向下箭头键向下滚动时,我可以使用什么等效的 .bind 方法来发出警报
我的尝试是(但不起作用):
$('#section1').bind('onkeydown', function (event) {
if (event.keyCode == 40) {
alert('scroll down with arrow key');
}
});
我非常感谢任何帮助。
我已经从以下位置查看了解决方案:JavaScript KeyCode Values are "undefined" in Internet Explorer 8
- 但是这样一来,我似乎无法针对特定的 <div>
发生此警报。 ,我不希望这种效果在整个文档中持续存在。
我也在使用.bind()
而不是任何其他方法,这是因为当我到达另一个 <div>
时我要.unbind()
前面的,因为在另一个 <div>
我想做一个新的.bind()
最佳答案
亨利我的问题是**
'onkeydown'
above keyword is the html tag's event attribute
you can check out the below reference link for this Reference link 'onkeydown' keyword
您用来绑定(bind)按键的关键字实际上只是
'keydown'
这是唯一的问题,否则你的代码是正确的
这是我对该代码的尝试。
$(document).on('keydown',function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 40) {
alert("down pressed");
} else if (code == 38) {
alert("up pressed");
}
});
这是您可以查看的工作代码的链接
Even working here with .bind() event to you can check out code
关于javascript - 使用向下箭头键的 .bind 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641922/