如何将文本附加到'
我有
例如,他输入“Hello, world!”,然后上传文件(通过 AJAX 完成),并且该文件的链接将添加到 < 内容的下一行中代码><文本区域>。注意力!是否可以将光标(他离开打字的地方)保留在同一个地方?
所有这一切都可以用 jQuery 来完成...有什么想法吗?我知道有方法“append()”,但它不适用于这种情况,对吗?
I have <textarea>
where user can type in his message to the world! Below, there are upload buttons... I would love to add link to uploaded file (don't worry, I have it); right next to the text that he was typing in.
Like, he types in 'Hello, world!', then uploads the file (its done via AJAX), and the link to that file is added in next line to the content of <textarea>
. Attention! Is it possible to keep cursor (place where he left to type) in the same place?
All that may be done with jQuery... Any ideas? I know that there are method 'append()', but it won't be for this situation, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
Try
这花了我一段时间,但是下面的 jQuery 会完全按照你的要求做——它不仅附加文本,而且还通过存储然后重置它来将光标保持在完全相同的位置:
你可能应该将其包装在一个函数中尽管。
This took me a while, but the following jQuery will do exactly what you want -- it not only appends text, but also keeps the cursor in the exact same spot by storing it and then resetting it:
You should probably wrap this in a function though.
您可以查看以下答案,其中提供了很好的插件,用于在
You may take a look at the following answer which presents a nice plugin for inserting text at the caret position in a
<textarea>
.您可以使用任何可用的插入符插件用于 jQuery,基本上:
如果您想要 jQuery 中的“附加”功能,则很容易制作:
然后您可以 通过调用
.valAppend()
引用输入字段来使用它 。You can use any of the available caret plugins for jQuery and basically:
If you want an "append" function in jQuery, one is easy enough to make:
Then you can use it by making a call to
.valAppend()
, referencing the input field(s).您可以使用我的Rangy input jQuery 插件来实现此目的,该插件适用于所有主要浏览器。
You could use my Rangy inputs jQuery plugin for this, which works in all major browsers.