我希望用户能够使用文本区域并通过按键控制文本区域。当文本区域被聚焦/被使用时,似乎与按键按下的检测有冲突。有好的解决办法吗?伪代码: keydown(function(event) { if (event.which == 11) { $(TextArea).focusout(); SwitchTextAreaFunction(); $(TextArea).focus(); } }
1 回答

慕哥9229398
TA贡献1877条经验 获得超6个赞
我想你想要这样的东西。
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>
添加回答
举报
0/150
提交
取消