我有一个“文本区域”。我想做一个计算器,但是在开始之前,我需要确保一切正常。

如果按“ 1”键,它将起作用。如果按“ /”键,则会打开“快速搜索”。
在Google Chrome浏览器中,我不能使用“ +和-”怎么办?



function doc_keyUp(e) {
  if (e.altKey && e.keyCode == 13 || e.keyCode == 49) {
    document.getElementById("area").value += "1";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 50) {
    document.getElementById("area").value += "2";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 51) {
    document.getElementById("area").value += "3";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 52) {
    document.getElementById("area").value += "4";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 53) {
    document.getElementById("area").value += "5";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 54) {
    document.getElementById("area").value += "6";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 55) {
    document.getElementById("area").value += "7";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 56) {
    document.getElementById("area").value += "8";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 57) {
    document.getElementById("area").value += "9";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 48) {
    document.getElementById("area").value += "0";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 61) {
    document.getElementById("area").value += "+";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 191) {
    document.getElementById("area").value += "/";
  }
  if (e.altKey && e.keyCode == 13 || e.keyCode == 173) {
    document.getElementById("area").value += "-";
    $('#area').val(textVal.substring(0, texVal.length = 1));
  }
}

document.addEventListener('keyup', doc_keyUp, false);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="area" onKeyPress="return validateKey(event)"></textarea>

最佳答案

对于“ +”和“-”,请尝试使用键代码187和189。
event.preventDefault(); -应该防止打开“快速搜索”。



<!DOCTYPE html>
<html>
<body>

<textarea id="area"></textarea>
<script>


function doc_keyUp(e) {
 if (e.altKey && e.keyCode == 13 || e.keyCode == 49) {
    document.getElementById("area").value += "1";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 50) {
    document.getElementById("area").value += "2";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 51) {
    document.getElementById("area").value += "3";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 52) {
    document.getElementById("area").value += "4";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 53) {
    document.getElementById("area").value += "5";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 54) {
    document.getElementById("area").value += "6";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 55) {
    document.getElementById("area").value += "7";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 56) {
    document.getElementById("area").value += "8";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 57) {
    document.getElementById("area").value += "9";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 48) {
    document.getElementById("area").value += "0";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 187) {
    document.getElementById("area").value += "+";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 191) {
    document.getElementById("area").value += "/";
   }
   if (e.altKey && e.keyCode == 13 || e.keyCode == 189) {
    document.getElementById("area").value += "-";
   }
e.preventDefault();
}
document.addEventListener('keyup', doc_keyUp, false);

</script>

</body>
</html>

关于javascript - 禁用键盘快捷键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59624937/

10-10 11:48