JQUERY将Textarea光标设置为文本结束 [英] JQUERY Set Textarea Cursor to End of Text

查看:145
本文介绍了JQUERY将Textarea光标设置为文本结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://jsfiddle.net/adamadam123/gEEVM/4/



我正在构建一个允许用户添加表情图标的聊天系统。

就像在上面的jsfiddler示例中,我将textarea中的当前文本与选定的表情图符号组合在一起,然后将此文本添加回textarea。 p>

  $(function(){
var data = $('textarea')。val();
var emoticon =':)';
$('textarea')。val(数据+图释);
$('textarea')。focus();
});

问题是,当我将焦点放回文本区域时,光标位于文本的开头。



如何将光标设置到文本的末尾 - 允许进一步输入?

解决方案

您可以做的很简单的事情就是重置文本区域的值:
$ b $ $ $ $ $ $ $ $ $ $(function() {
var data = $('textarea')。val();
var emoticon =':)';
$('textarea')。focus()。val('')。val(data + emoticon);
});


http://jsfiddle.net/adamadam123/gEEVM/4/

I'm building a chat system that allows users to add emoticons.

Just like in the jsfiddler example above I take the current text in the textarea, combine it with the chosen emoticon symbol and then add this text back into the textarea.

$(function() {
    var data = $('textarea').val();
    var emoticon = ':)';
    $('textarea').val(data + emoticon);
    $('textarea').focus();
});  

The problem is when I set the focus back into the textarea the cursor is at the beginning of the text.

How can I set the cursor to the end of the text - to allow further typing?

解决方案

Something simple you can do is reset the text area's value:

$(function() {
    var data = $('textarea').val();
    var emoticon = ':)';
    $('textarea').focus().val('').val(data + emoticon);
}); 

这篇关于JQUERY将Textarea光标设置为文本结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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