移动光标到相应的文本(比如项目中,插入了图片之后,即在插入的地方显示光标)

       在选定的文本框中,点击了光标,进行了额外的操作,比如说,进行图片的插入、表情的插入之后,如果是直接在改dom元素中进行focus操作的话,光标的位置会直接放置文本的最后,而不是我们期望的放置在插入位置的最后。
要想改变这个光标的正确的返回位置的话,那么就得封装一个方法,用来对光标位置的移动。

项目中的代码如下:(该项目建立在vue的基础上,用vue2的注册组件方法ref绑定了元素进行操作,具体看如下的注释)

          ...
        sendEmoji (item) {//点击了标签图片的方法
            let _self = this,//将vue用变量存储
            msgTextarea = _self.$refs.msg_textarea,//vue的注册组件以后,调用DOM的方法
            cursorIndex = 0,//光标在文本框中的位置
            comment =  _self.comment;//vue中的评论内容
            if (document.selection) {
                // IE Support(测试了一下,ie11也不走这里了,其它版本的ie没有细看)
                msgTextarea.focus ();
                let range = document.selection.createRange();
                range.moveStart ('character', -msgTextarea.value.length);
                cursorIndex = range.text.length;
            }else if (msgTextarea.selectionStart || msgTextarea.selectionStart==0) {
                // another support
                cursorIndex = msgTextarea.selectionStart;    
            }
            let commentLight = comment.substring(0, cursorIndex) + '【em'+item+'】';
            _self.comment = commentLight + comment.substring(cursorIndex, comment.length);
            this.$nextTick(function(){//方法解决异步问题, 获取更新后的DOM
                msgTextarea.focus();
                msgTextarea.selectionStart = msgTextarea.selectionEnd = commentLight.length;//这是最终的状态让 msgTextarea.selectionStart跟 msgTextarea.selectionEnd相等,这里是关键,必须要获取到对的位置
            })
            this.emojiShow = false;
        },
           ...



jq版:(这里可以修改成为js的代码,可以直接利用)
//文本框光标输入
          (function($) {
              $.fn.insertContent = function(myValue, t) {                 
                   var $t = $(this)[0];
                   if (document.selection) { //ie
                        this.focus();
                        var sel = document.selection.createRange();
                        sel.text = myValue;
                        this.focus();
                        sel.moveStart('character', -l);
                        var wee = sel.text.length;
                        if (arguments.length == 2) {
                             var l = $t.value.length;
                             sel.moveEnd("character", wee + t);
                             t <= 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart("character", wee - t - myValue.length);
          
                             sel.select();
                        }
                   } 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;
                        if (arguments.length == 2) {
                             $t.setSelectionRange(startPos - t, $t.selectionEnd + t);
                             this.focus();
                        }
                   }
                   else {
                        this.value += myValue;
                        this.focus();
                   }
             };
          })(jQuery);   
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值