function doGetCaretPosition(oField) {
// Initialize
let iCaretPos = 0
// IE Support
if (document.selection) {
// Set focus on the element
oField.focus()
// To get cursor position, get empty selection range
let oSel = document.selection.createRange()
// Move selection start to 0 position
oSel.moveStart('character', -oField.value.length)
// The caret position is selection length
iCaretPos = oSel.text.length
}
// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0')
iCaretPos = oField.selectionDirection == 'backward' ? oField.selectionStart : oField.selectionEnd;
// Return results
return iCaretPos
}
##资料 Get cursor position (in characters) within a text Input field