更改HTML文本框:覆盖而不是作为用户类型插入

问题描述:

我正在开发一种允许编辑文本的服务.为了帮助用户进行此过程,我希望允许用户将文本字段设置为覆盖模式(如Word等).如何将HTML文本框的行为更改为覆盖而不是插入文本用户输入的类型?

I am working on a service which will allow editing of text. To aid the user in the process, I'd like to allow the user to set a text field to overwrite mode, as is possible in Word, etc. How can the behaviour of an HTML text box be changed to overwrite instead of insert text as the user types?

例如,如果文本框中包含以下文本:

For example, if the textbox had the text:

This is a trst.

用户可以在r和t之间单击,键入一个e,然后输入文本

The user could click between the r and the t, type a single e and the text would then be

This is a test.

,光标位于es之间.我目前使用的是jQuery,因此首选使用那个或纯JavaScript的方法.但是,我会接受任何合理的解决方案.

with the cursor between the e and the s. I'm currently using jQuery, so methods using either that or pure javascript would be preferred. I would accept any reasonable solution, however.

这有点疯狂,但它似乎可以以某种方式起作用:)

That's a bit of crazy but it seems to work somehow :)

基于此答案

演示: http://jsfiddle.net/aHSzC/

它可以工作,但是现在我没有时间修复发现的小错误.

It works but now I have no time to fix a small bug I found.

  • 如果按 Backspace ,则其行为类似于前向橡皮擦.
  • If you press Backspace it seems to behave like a forward eraser.

无论如何,这是可以改进的代码.随时编辑我的答案,然后随心所欲.

Anyway, here is the code that can be improved. Feel free to edit my answer and do whatever you like.