禁用对文本区域的关注或按键输入的最佳方法是什么?
What is the best way to disable focus on a Textarea or Input on a keydown?
我希望用户能够使用文本区域并通过按键控制文本区域。当文本区域被聚焦/被使用时,似乎与按键按下的检测有冲突。有好的解决办法吗?
伪代码:
keydown(function(event) {
if (event.which == 11) {
$(TextArea).focusout();
SwitchTextAreaFunction();
$(TextArea).focus();
}
}
我想你想要这样的东西。
document.addEventListener('keydown', (e) => {
const myTextArea = $("#myTextArea");
if (e.keyCode === 13 /* 13=enter */ ) {
if (!myTextArea.is(":focus")) {
// textarea has no focus
e.preventDefault();
myTextArea.focus();
} else {
// textarea has focus
myTextArea.blur();
}
}
})
<textarea id="myTextArea">TextArea</textarea>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
我希望用户能够使用文本区域并通过按键控制文本区域。当文本区域被聚焦/被使用时,似乎与按键按下的检测有冲突。有好的解决办法吗?
伪代码:
keydown(function(event) {
if (event.which == 11) {
$(TextArea).focusout();
SwitchTextAreaFunction();
$(TextArea).focus();
}
}
我想你想要这样的东西。
document.addEventListener('keydown', (e) => {
const myTextArea = $("#myTextArea");
if (e.keyCode === 13 /* 13=enter */ ) {
if (!myTextArea.is(":focus")) {
// textarea has no focus
e.preventDefault();
myTextArea.focus();
} else {
// textarea has focus
myTextArea.blur();
}
}
})
<textarea id="myTextArea">TextArea</textarea>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>