在javascript替换中停止光标跳到输入字段的末尾 [英] Stop cursor from jumping to end of input field in javascript replace

查看:18
本文介绍了在javascript替换中停止光标跳到输入字段的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式从 javascript(在 IE 中运行)的文本输入区域中去除无效字符.我在每个 keyup 事件上运行替换功能.但是,这会使光标在每次按键后跳转到文本框的末尾,从而无法进行内联编辑.

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.

实际效果如下:

http://jsbin.com/ifufuv/2

有谁知道怎么做才能让光标不跳到输入框的末尾?

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替换中停止光标跳到输入字段的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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