如何使用Javascript在TextArea中在光标位置附加文本 [英] How to append text in TextArea at cursor location using Javascript

查看:101
本文介绍了如何使用Javascript在TextArea中在光标位置附加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextArea,textBox和一个按钮。 TextArea有一些文本,例如这是一只猫。

I have a TextArea, textBox and a button. TextArea has some text e.g This is a cat.

现在我的要求是如果有人在TextArea中设置光标位置并在文本框中输入文本并单击按钮,则应附加文本在光标位置而不是最后。例如。

Now my requirement is If someone set cursor position in TextArea and enter text in textbox and click on button the text should be append in cursor position instead of last. e.g.

TextArea:这是一只猫。
光标位置:在a
之后在TextBox中输入文本:黑色

TextArea: This is a cat. Cursor position: after "a" Entered Text in TextBox: black

输出:这是一只黑猫。

如何使用javascript执行此操作。

How can I do this using javascript.

提前致谢。

推荐答案

我之前已经回答过:

在文本区域的光标处插入文本,使用Javascript

另外需要注意的是,当点击事件触发按钮时,IE将丢失插入位置。要绕过这个,您可以使用 mousedown 事件,或者通过添加 unselectable =on属性。

One extra note is that IE will lose the caret position by the time a click event fires on a button. To get round this you can either use themousedown event instead, or make the button unselectable by adding an unselectable="on" attribute.

这篇关于如何使用Javascript在TextArea中在光标位置附加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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