js获取光标位置并插入内容

先来几个网上找的参考资源,我爱互联网,互联网使我变得更加强大。

https://blog.csdn.net/mafan121/article/details/78519348 详细篇,该作者很用心的解释了每一个api的用法。

https://blog.csdn.net/smartsmile2012/article/details/53642082 https://blog.csdn.net/liushuijinger/article/details/48834541 在光标位置插入内容

https://blog.csdn.net/wangzhikui1/article/details/52236091 设置光标的出现的位置

https://segmentfault.com/q/1010000006149636 一个思路清奇的朋友写的基于vue的光标插入内容

接下来就是我自己基于以上材料思路自己写的基于vue2.0的了。

getCursorPosition (event) {
            const editEl = event.target;
            // return console.log(editEl);
            if (editEl.selectionStart ||editEl.selectionStart === 0) {
                // 非IE浏览器
                this.cursorPosition = editEl.selectionStart;
            } else {
                // IE
                const range = document.selection.createRange();
                range.moveStart('character', -editEl.value.length);
                this.cursorPosition = range.text.length;
            }
            console.log(this.cursorPosition);
        }

  

原文地址:https://www.cnblogs.com/webSong/p/9861102.html