【发布时间】:2014-06-21 12:40:08
【问题描述】:
我正在使用一个函数来显示一个 div:
function showCMD() {
$("#cmd").show("fast");
$('#cmdText').focus();
}
如果用户在键盘上键入“cmd”,就会发生这种情况。
主要代码:
document.onkeyup = function(event) {
//Save the last three keys
one = two;
two = three;
three = event.keyCode;
if (one == 67 && two == 77 && three == 68 && cmdOpen == false) {
showCMD();
}
//if the pressed key is ENTER and the textarea is focused
if (event.keyCode == 13 && $("#cmdText").is(":focus") == true) {
//passing the code to another function
execCMD(document.getElementById("cmdText").value);
//empty the textarea - works great
document.getElementById("cmdText").value = "";
return false;
}
}
用户输入的代码会在这里处理:
function execCMD(command) {
if(command == "exit") $("#cmd").hide("fast");
console.log(command);
}
控制台每次退出时都会给我。但它不会隐藏 div #cmd。 如果我将 onkeyup 更改为 onkeydown,它可以正常工作。 onkeydown 的问题是,在使用键序列“cmd”打开命令行后,textarea 显示“d”。
所以我要么无法关闭#cmd,要么每次打开#cmd 时都会显示“d”。
最后但并非最不重要的html代码:
<div id="cmd">
<textarea id="cmdText" maxlength="80"></textarea>
</div>
也许你会知道我的问题的解决方案!到目前为止谢谢你
【问题讨论】:
-
你也应该小心使用 '==' 而使用 '===' 来避免类型转换。见这篇文章:stackoverflow.com/questions/359494/…
标签: javascript jquery html onkeydown onkeyup