Javascript - 在写下 textarea 之前更改输入字母 [英] Javascript - Change input letter before written down textarea

查看:28
本文介绍了Javascript - 在写下 textarea 之前更改输入字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 jQuery 制作一个虚拟键盘.

I'm trying to make a virtual keyboard with jQuery.

当我在给定的文本区域中按 'a' 时,假设应该写成 'z'.

When I press 'a' in the given textarea, suppose that 'z' should be written instead.

在textarea中,我发现打字的整个步骤是

In textarea, I found the whole steps of typing a letter is given by

keyDown 事件 → keyPress 事件 → textarea 中输入的字母 → keyUp 事件

在我的代码中,我检测到 keyPress 事件 中的字母,在那个事件中,如果字母是 'a',我添加了 'z' 在 textarea 中使用 .val() 方法.

In my code, I detected the letter in keyPress event and in that event, if the letter was 'a', I added 'z' in textarea using .val() method.

如果我们在textarea中有初始字符串hello,在keyPress event之后,我们有helloz.然后,在keyUp事件发生之前,textarea变成了helloza.所以我删除了 keyUp event 中的最后一个字母 'a'.

If we have the initial string hello in textarea, after keyPress event, we have helloz. Then, before keyUp event occurs, textarea became helloza. So I deleted the last letter, 'a' in keyUp event.

这看起来很棒,而且效果很好.但是,问题是时间延迟.

This looks great, and also works well. But, the problem is TIME DELAY.

由于我上面写的步骤需要一秒钟,'a' 在 textarea 中出现了很长时间.

Since steps I wrote above takes a second, 'a' appears quite a while in the textarea.

但是 Google 翻译器 中的虚拟键盘不显示原始字母.

But the virtual keyboard in Google Translator does not show the original letter.

我该怎么做?

推荐答案

尝试在 keyPress 事件处理程序中调用 event.preventDefault().

Try calling event.preventDefault() in the keyPress event handler.

这篇关于Javascript - 在写下 textarea 之前更改输入字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆