JS获取及设置TextArea或input文本框选择文本位置的

function getPos(el) { var range, textRange, duplicate el.focus() if ( el.selectionStart ) return el.selectionStart else if ( document.selection ) { // IE range = document.selection.createRange() if ( range == null ) return el.value.length textRange = el.createTextRange() duplicate = textRange.duplicate() textRange.moveToBookmark(range.getBookmark()) duplicate.setEndPoint('EndToStart', textRange) return duplicate.text.length } } function setPos(el, pos) { var range el.focus() if ( el.setSelectionRange ) el.setSelectionRange(pos, pos) else if ( el.createTextRange ) { range.collapse(true) range.moveEnd('character', pos) range.moveStart('character', pos) range.select() } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgfyfs.html