您的位置:首页 > 其它

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

2017-12-09 13:12 399 查看
       在选定的文本框中,点击了光标,进行了额外的操作,比如说,进行图片的插入、表情的插入之后,如果是直接在改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);   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐