gpt4 book ai didi

javascript - 使用 JQuery/Javascript 时,Delete 和 Dot 的键码是否相同?

转载 作者:行者123 更新时间:2023-12-01 05:37:56 25 4
gpt4 key购买 nike

我正在 try catch 点和删除的键码,但在不同的事件上。

function validateSomething(eve) {
var charCode = (eve.which) ? eve.which : eve.keyCode
if (charCode == 46)
console.log("Killer Brand")
}

当我在文本框中按 DOT 或删除时;按键时调用上述函数; if 语句对两者都执行。为什么?那怎么解决呢?

最佳答案

改用 keydown 事件。正如 W3schools 所说:

The onkeypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers. To detect only whether the user has pressed a key, use the onkeydown event instead, because it works for all keys

因此可以通过按键时使用相同的代码来检测它。在这里阅读更多内容:

所以,这是一个有效的示例:http://www.w3schools.com/jsref/event_onkeypress.asp

<input type="text" onkeydown="validateSomething(event)" />

function validateSomething(eve) {
var charCode = (eve.which) ? eve.which : eve.keyCode
if (charCode == 46) {
console.log("Killer Brand")
}
}

关于javascript - 使用 JQuery/Javascript 时,Delete 和 Dot 的键码是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com