如何将文本附加到'

发布于 2024-11-25 03:13:33 字数 321 浏览 1 评论 0原文

我有

例如,他输入“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

披肩女神 2024-12-02 03:13:33

尝试

var myTextArea = $('#myTextarea');
myTextArea.val(myTextArea.val() + '\nYour appended stuff');

Try

var myTextArea = $('#myTextarea');
myTextArea.val(myTextArea.val() + '\nYour appended stuff');
烟雨扶苏 2024-12-02 03:13:33

这花了我一段时间,但是下面的 jQuery 会完全按照你的要求做——它不仅附加文本,而且还通过存储然后重置它来将光标保持在完全相同的位置:

var selectionStart = $('#your_textarea')[0].selectionStart;
var selectionEnd = $('#your_textarea')[0].selectionEnd;

$('#your_textarea').val($('#your_textarea').val() + 'The text you want to append');

$('#your_textarea')[0].selectionStart = selectionStart;
$('#your_textarea')[0].selectionEnd = selectionEnd;

你可能应该将其包装在一个函数中尽管。

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:

var selectionStart = $('#your_textarea')[0].selectionStart;
var selectionEnd = $('#your_textarea')[0].selectionEnd;

$('#your_textarea').val($('#your_textarea').val() + 'The text you want to append');

$('#your_textarea')[0].selectionStart = selectionStart;
$('#your_textarea')[0].selectionEnd = selectionEnd;

You should probably wrap this in a function though.

2024-12-02 03:13:33

您可以查看以下答案,其中提供了很好的插件,用于在

You may take a look at the following answer which presents a nice plugin for inserting text at the caret position in a <textarea>.

友谊不毕业 2024-12-02 03:13:33

您可以使用任何可用的插入符插件用于 jQuery,基本上:

  1. 存储当前插入符位置
  2. 将文本附加到文本区域
  3. 使用插件替换插入符位置

如果您想要 jQuery 中的“附加”功能,则很容易制作:

(function($){
    $.fn.extend({
        valAppend: function(text){
            return this.each(function(i,e){
                var $e = $(e);
                $e.val($e.val() + text);
            });
        }
    });
})(jQuery);

然后您可以 通过调用 .valAppend() 引用输入字段来使用它

You can use any of the available caret plugins for jQuery and basically:

  1. Store the current caret position
  2. Append the text to the textarea
  3. Use the plugin to replace the caret position

If you want an "append" function in jQuery, one is easy enough to make:

(function($){
    $.fn.extend({
        valAppend: function(text){
            return this.each(function(i,e){
                var $e = $(e);
                $e.val($e.val() + text);
            });
        }
    });
})(jQuery);

Then you can use it by making a call to .valAppend(), referencing the input field(s).

晒暮凉 2024-12-02 03:13:33

您可以使用我的Rangy input jQuery 插件来实现此目的,该插件适用于所有主要浏览器。

var $textarea = $("your_textarea_id");
var sel = $textarea.getSelection();
$textarea.insertText("\nSome text", sel.end).setSelection(sel.start, sel.end);

You could use my Rangy inputs jQuery plugin for this, which works in all major browsers.

var $textarea = $("your_textarea_id");
var sel = $textarea.getSelection();
$textarea.insertText("\nSome text", sel.end).setSelection(sel.start, sel.end);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文