更改HTML文本框:覆盖而不是作为用户类型插入 [英] Change HTML Textbox: Overwrite Instead of Insert as User Types

查看:110
本文介绍了更改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.

这篇关于更改HTML文本框:覆盖而不是作为用户类型插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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