点击后,表情符号到textarea

时间:2012-06-17 11:07:28

标签: javascript jquery html onclick textarea

我有一个小问题

我想点击我的表情并将文字插入文本区域。 当我想稍后添加笑脸时,应将笑脸添加到光标位置,而不是添加到textarea的末尾。

这是我的 html 代码:

<textarea id="description" name="description"></textarea>

<div id="emoticons">
    <a href="#" title=":)"><img alt=":)" border="0" src="/images/emoticon-happy.png" /></a>
    <a href="#" title=":("><img alt=":(" border="0" src="/images/emoticon-unhappy.png" /></a>
    <a href="#" title=":o"><img alt=":o" border="0" src="/images/emoticon-surprised.png" /></a>
</div>

这是我的 JS 代码:

$('#emoticons a').click(function(){

    var smiley = $(this).attr('title');
    $('#description').val($('#description').val()+" "+smiley+" ");

});

在这里你可以看到结果(NO WRAP - BODY) http://jsfiddle.net/JVDES/8/

我使用extern JS文件作为我的javascript代码...
你知道为什么代码没有在NO WRAP - HEAD模式下运行吗? http://jsfiddle.net/JVDES/9/

感谢您的帮助

关心bernte

4 个答案:

答案 0 :(得分:7)

此外,您可以使用此功能:

function ins2pos(str, id) {
   var TextArea = document.getElementById(id);
   var val = TextArea.value;
   var before = val.substring(0, TextArea.selectionStart);
   var after = val.substring(TextArea.selectionEnd, val.length);
   TextArea.value = before + str + after;
}

对于您的示例,请查看here


UPD 1:

要将光标设置在指定位置,请使用下一个功能:

function setCursor(elem, pos) {
   if (elem.setSelectionRange) {
      elem.focus();
      elem.setSelectionRange(pos, pos);
   } else if (elem.createTextRange) {
      var range = elem.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
   }
}

我在jsFiddle上更新了代码,因此,要查看新示例,请查看here

答案 1 :(得分:3)

试试这个 - http://jsfiddle.net/JVDES/12/

插入位置功能的信用值为 - https://stackoverflow.com/a/1891567/981134

答案 2 :(得分:3)

no wrap (head)no wrap (body)之间的区别在于前者会尽快运行代码,而后者会在页面加载后运行代码。

在这种情况下,使用no wrap (head),代码将在“#emoticons a”存在之前运行。因此$('#emoticons a')返回none并且不附加click处理程序。之后,链接已创建。

因此,解决方案是告诉代码在页面加载时运行。

有几种相同的方法可以做到这一点。 http://api.jquery.com/ready/

  1. $(document).ready(handler)

  2. $().ready(handler)(不建议这样做)

  3. $(handler)

  4. 因此,使用版本3,我们有:

    $(function() {
        $('#emoticons a').click(function(){
    
        var smiley = $(this).attr('title');
        $('#description').val($('#description').val()+" "+smiley+" ");
    
        });
    });
    

    http://jsfiddle.net/Nc67C/

答案 3 :(得分:2)

也试试这个......

$('#emoticons a').click(function() {

    var smiley = $(this).attr('title');

    var cursorIndex = $('#description').attr("selectionStart");
    var lStr =  $('#description').val().substr(0,cursorIndex);
    var rStr = $('#description').val().substr(cursorIndex);

    $('#description').val(lStr+" "+smiley+" "+rStr);

});

在您的评论后修复:

$('#emoticons a').click(
function(){var smiley = $(this).attr('title');

var cursorIndex = $('#description').attr("selectionStart");
var lStr =  $('#description').val().substr(0,cursorIndex) + " " + smiley + " ";
var rStr = $('#description').val().substr(cursorIndex);

$('#description').val(lStr+rStr);
$('#description').attr("selectionStart",lStr.length);                                     
});​