如何知道哪个字符被退格删除或删除 [英] How to know which character(s) is(are) get deleted on backspace or delete

查看:63
本文介绍了如何知道哪个字符被退格删除或删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文本区域中编辑文本时,如何知道在退格中删除了哪些字符或单击了删除按钮.

How to know which character(s) is(are) get deleted on backspace or delete button click when editing text in textarea.

我想让字符在退格键上删除或单击删除按钮以检测删除的字符是否为换行符.

I want to get character get deleted on backspace or delete button click to detect if deleted character is new line character.

推荐答案

那么您可以做两件事.您可以存储当前值,然后将其与新值进行比较.或者,您可以尝试在事件真正完成之前捕获事件.

Well you can do 2 things. You could either store the current value and compare it to the new value. Or you can try to catch the event before it really does what it has to do.

我发现这可以防止退格功能此处检查它是否对您有用.您可以使用SelectionStart函数来找出光标的位置.通过检查键码,可以确定是否按下了退格键或删除键.删除将表示:位置+1是要删除的字符.退格键表示:位置-1是要删除的字符.

I found this prevent backspace function here check if its any use of you. You can use the SelectionStart function to find out what the position of your cursor is. By checking the keycode you can determine if the backspace or delete button was pressed. Delete will mean: position +1 is the character being deleted. Backspace will mean: position -1 is the character being deleted.

由于您要防止退格和/或删除操作来触发其原始事件,因此您一直有时间找出这些问题.知道所有需要了解的信息后,您可以手动从字符串中删除字符并更新文本区域中的文本.

Because you are preventing backspace and/or delete to trigger their original event you have all the time to find those things out. Once you know all the information you needed to know you can manually remove the character from the string and update the text in the textarea.

不确定这是否是最好的方法,但似乎可以像这样工作.

Not sure if its the best way but it seems like something that could work like this.

希望这对您有任何帮助.

Hope this helped you in any way.

这篇关于如何知道哪个字符被退格删除或删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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