下面的按键功能会调用某个功能,具体取决于按下给定键的次数。执行时,只运行第一个if语句,第二个不运行。
$(document).keypress(function(number) {
var pressCount = 0;
pressCount++;
if (number.which == 67 || number.which == 99) {
if (pressCount = 1) {
callThisFunction();
} else if (pressCount = 2) {
callThisOtherFunction();
}
}
});
答案 0 :(得分:3)
它可能正在发生,因为" pressCount"每次函数运行时都会重置为0.
尝试以下调整:
var pressCount = 0;
$(document).keypress(function(number) {
pressCount++;
if (number.which == 67 || number.which == 99) {
if (pressCount == 1) {
callThisFunction();
} else if (pressCount == 2) {
callThisOtherFunction();
}
}
});
答案 1 :(得分:1)
您在=
声明中遗漏了if
个符号。改为:
if (pressCount == 1) {
callThisFunction();
} else if (pressCount == 2) {
callThisOtherFunction();
}
同时在函数之外声明pressCount
变量。否则,每按一次键,它都会重置为0.
var pressCount = 0;
$(document).keypress(function(number) {
... // rest of your code