在文本输入字段上模拟退格键 [英] simulate backspace key on text input field

查看:188
本文介绍了在文本输入字段上模拟退格键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供一个很好的例子来模拟< INPUT> 文本字段上的退格键吗?



我需要在HTML5中使用虚拟键盘,并且只需要在 Webkit 浏览器中使用。



注释:


  • createEvent / initTextEvent / dispatchEvent 与charcode 8(backspace)被忽略(这似乎只适用于可打印字符)

  • 使用 substr()手动更改 但这样插入符就会移到输入字段的末尾(我希望能够删除输入字段中间的字符,就像使用普通键盘时一样)。
  • 在这里你可以去:

nofollow的> http://jsfiddle.net/dan_barzilay/WWAh7/2/



如果您需要更多解释注释,get caret函数用于知道删除角色的位置。
重置游标函数用于在退格后恢复插入位置。



**它使用jquery,但您可以将其更改为正常的js, / p>

Can someone provide a good example to simulate the backspace key on a <INPUT> text field?

I need this for a virtual keyboard done in HTML5 and it only needs to work in Webkit browsers.

Notes:

  • createEvent/initTextEvent/dispatchEvent with charcode 8 (backspace) is being ignored (that seems to work only for printable characters)
  • changing manually the value using substr() sort of works, but this way the caret moves to the end of the input field (and I want to be able to delete characters in the middle of the input field, just like when using a normal keyboard).

解决方案

Alright here you go: http://jsfiddle.net/dan_barzilay/WWAh7/2/

The get caret function is used to know where to delete the character, if you need more explaining comment. The reset cursor function is used to restore the caret position after the backspacing.

**it's using jquery but you can change it to normal js in secounds

这篇关于在文本输入字段上模拟退格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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