jQuery plugin:得到/设置textarea中光标的位置

韩峰
2023-12-01
jQuery.fn.extend({
    getCurPos: function(){
        var e=$(this).get(0);
        e.focus();
        if(e.selectionStart){    //FF
            return e.selectionStart;
        }
        if(document.selection){    //IE
            var r = document.selection.createRange();
            if (r == null) {
                return e.value.length;
            }
            var re = e.createTextRange();
            var rc = re.duplicate();
            re.moveToBookmark(r.getBookmark());
            rc.setEndPoint('EndToStart', re);
            return rc.text.length;
        }
        return e.value.length;
    },
    setCurPos: function(pos) {
        var e=$(this).get(0);
        e.focus();
        if (e.setSelectionRange) {
            e.setSelectionRange(pos, pos);
        } else if (e.createTextRange) {
            var range = e.createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }
    }        
});


转载于:https://my.oschina.net/txijin/blog/33998

 类似资料: