input获取焦点位置

获取input当前焦点位置插入值

selectionStart

方法例子

1
2
3
4
5
6
7
8
9
10
11
12
getPosition = element => {
let cursorPos = 0;
if (document.selection) {
//IE
var selectRange = document.selection.createRange();
selectRange.moveStart('character', -element.value.length);
cursorPos = selectRange.text.length;
} else if (element.selectionStart || element.selectionStart === '0') {
cursorPos = element.selectionStart;
}
return cursorPos;
};