我有下面的代码,可以在按键时触发各种事件
$('#input-div').keydown(function(e) {
if(e.which == 13) {
userInput = $(this).val();
$(this).val(userInput.substring(0,0));
这会清除输入框。但是由于按下回车键,光标转到第二行。我希望在释放回车键后光标回到初始位置;上键。
有什么帮助吗?
最佳答案
只需添加e.preventDefault()
:
$('#input-div').keydown(function(e) {
if(e.which == 13) {
userInput = $(this).val();
$(this).val(userInput.substring(0,0))
e.preventDefault()
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="input-div"></textarea>
关于javascript - 如何使用 jQuery 触发退格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42605345/