gpt4 book ai didi

javascript - 在我的计算机上无法运行检测文本区域中的 DEL 键的相同代码?

转载 作者:行者123 更新时间:2023-11-28 19:57:35 27 4
gpt4 key购买 nike

我想检测用户何时按下Delete键,我找到了教程here

该代码在 jsfiddle.net 中运行良好,这是链接 - http://jsfiddle.net 。我将相同的代码复制到我的计算机上,然后进行了测试,但效果不佳。

我在这里发布我的代码:

<html>

<script src="./jquery-1.10.2.min.js"></script>


<body>
<script language="javascript" type="text/javascript">

var specialChars = {
27: "esc", 9: "tab", 32: "space", 13: "return", 8: "backspace", 145: "scroll", 20: "capslock", 144: "numlock",
19: "pause", 45: "insert", 36: "home", 46: "del", 35: "end", 33: "pageup", 34: "pagedown",
37: "left", 38: "up", 39: "right", 40: "down", 109: "-",
112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6",
118: "f7", 119: "f8", 120: "f9", 121: "f10", 122: "f11", 123: "f12", 191: "/"
};

function chromeKeyPress(i,e){
e.type="chromekeypress";
e.which = 0;
handleKey(e);
}
function handleKey(e) {
$("#r").html($("#r").html() + "\n" +
e.type + "\n" +
" which: " + e.which + " == " + String.fromCharCode(e.which) + "\n" +
" keyCode: " + e.keyCode + " == " + String.fromCharCode(e.keyCode) + "\n" +
"");
}

$("textarea").keydown(function(e) {
if (specialChars[e.keyCode])
{
$("textarea").trigger("chromekeypress", e);

$("textarea").unbind("keypress");
setTimeout(function(){ $("textarea").bind("keypress", handleKey); },10);
}
});
$("textarea").keypress(handleKey);
$("textarea").bind("chromekeypress", chromeKeyPress);

</script>


<textarea></textarea>

<pre id="r"></pre>


</body>
</html>

我链接了jqueryjquery-1.10.2.min.js。我的 javascript 代码有什么问题。

最佳答案

您可以将代码包装在 DOM 就绪处理程序中 $(document).ready(function() {....}); 或更短的形式 $(function) {.. .}); 确保在执行 jQuery 代码之前所有 DOM 元素都已正确加载:

$(function() {
// Your jQuery code here
});

您的代码可以在 jsFiddle 上运行,因为当您从 jsFiddle 包含 jQuery 时,上述任务已自动完成

关于javascript - 在我的计算机上无法运行检测文本区域中的 DEL 键的相同代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22272215/

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