在javascript replace中停止光标跳转到输入字段的结尾 [英] Stop cursor from jumping to end of input field in javascript replace
问题描述
我正在使用正则表达式从javascript(在IE中运行)的文本输入区域中删除无效字符。我在每个keyup事件上运行replace函数。但是,这会使光标在每次按键后跳转到文本框的末尾,这使得无法进行内联编辑。
I'm using a regular expression to strip invalid characters out of an text input area in javascript (running in IE). I run the replace function on every keyup event. However, this makes the cursor jump to the end of the text box after each keypress, which makes inline editing impossible.
以下是它的实际应用:
有没有人知道如何制作它以便光标做到没有跳转到输入框的末尾?
Does anyone know how to make it so the cursor does not jump to the end of the input box?
推荐答案
您必须手动将光标放回原点。对于IE9,设置 .selectionStart
和 .selectionEnd
(或使用 .setSelectionRange(start,end )
)。对于IE8及更早版本,请使用 .createTextRange()
并在文本范围内调用 .moveStart()
。
You'll have to manually put the cursor back where you want it. For IE9, set .selectionStart
and .selectionEnd
(or use .setSelectionRange(start, end)
). For IE8 and earlier, use .createTextRange()
and call .moveStart()
on the text range.
这篇关于在javascript replace中停止光标跳转到输入字段的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!