$.fn.extend({ setCaretPosition : function(pos) { // 设置光标 var ctrl = this[0]; if (ctrl.setSelectionRange) { ctrl.focus(); ctrl.setSelectionRange(pos, pos); } else if (ctrl.createTextRange) { var range = ctrl.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); } }, getCursortPosition : function() { // 获取光标 var CaretPos = 0; // IE Support var ctrl = this[0]; if (document.selection) { ctrl.focus(); var Sel = document.selection.createRange(); Sel.moveStart('character', -ctrl.value.length); CaretPos = Sel.text.length; } else if (ctrl.selectionStart || ctrl.selectionStart == '0') { // FF // Support CaretPos = ctrl.selectionStart; return (CaretPos); } } });