先來幾個網上找的參考資源,我愛互聯網,互聯網使我變得更加強大。
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); }