zoukankan      html  css  js  c++  java
  • 在textarea指定位置插入字符或表情

    (function($){
     $.fn.extend({
      insertAtCaret: function(myValue){
       var $t=$(this)[0];
       if (document.selection) {
        this.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        this.focus();
       }
       else
        if ($t.selectionStart || $t.selectionStart == '0') {
         var startPos = $t.selectionStart;
         var endPos = $t.selectionEnd;
         var scrollTop = $t.scrollTop;
         $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
         this.focus();
         $t.selectionStart = startPos + myValue.length;
         $t.selectionEnd = startPos + myValue.length;
         $t.scrollTop = scrollTop;
        }
        else {
         this.value += myValue;
         this.focus();
        }
      }
     }) 
    })(jQuery);

    [代码]调用方法     

    1 $("#textareaId").insertAtCaret("新表情");
  • 相关阅读:
    Java程序设计作业02
    Java程序设计作业01
    DS博客作业05
    DS博客作业04
    DS博客作业03
    DS博客作业02
    DS博客作业01
    C博客作业06
    C博客作业05
    C语言——数组作业批改
  • 原文地址:https://www.cnblogs.com/liziqiang/p/3413782.html
Copyright © 2011-2022 走看看