好吧,我的问题(我希望)相当简单。我想知道该怎么做,以便我可以为相同的键码创建不同的事件。例如,我想在第一次按键时淡出一个 div 并淡入新的 div,然后在按键时淡出该 div 并淡入新的 div。
谢谢!
$(document).keydown(function() {
if (event.keyCode == '40') {
$('.div-1').fadeOut("slow")
$('.div-2').fadeIn("slow")
// I'd like this event to occur on the Second keydown
$('.div-2').fadeOut("slow")
$('.div-3').fadeIn("slow")
}
});
最佳答案
尝试
var hits = 0;
$(document).keydown(function() {
if (event.keyCode == '40') {
hits++;
if (hits % 2 == 0) {
// I'd like this event to occur on the Second keydown
$('.div-2').fadeOut("slow");
$('.div-3').fadeIn("slow");
} else {
$('.div-1').fadeOut("slow");
$('.div-2').fadeIn("slow");
}
});
关于javascript - 多个 Keydown 事件的不同操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2955033/