Javascript 커서 위치 가 져 오기

2632 단어 JavaScript

$.fn.extend({   
   position:function( pos, text ){   
       var elem = this[0];   
       if (elem&&(elem.tagName=="TEXTAREA"||elem.type.toLowerCase()=="text")) {   
           if($.browser.msie){   
               var rng;   
                if(elem.tagName == "TEXTAREA"){    
                    rng = event.srcElement.createTextRange();   
                    rng.moveToPoint(event.x,event.y);   
                }else{    
                    rng = document.selection.createRange();   
                }   
                if( pos === undefined ){   
                    rng.moveStart("character",-event.srcElement.value.length);   
                    return  rng.text.length;   
                }else if(typeof pos === "number" ){   
                    var index=this.position();   
                    index>pos?( rng.moveEnd("character",pos-index)):(rng.moveStart("character",pos-index))   
                    rng.select();   
                    if( text === undefined ) {   
                        return pos;   
                    }   
                }   
            }else{   
                if( pos === undefined ){   
                    return elem.selectionStart;   
                }else if(typeof pos === "number" ){   
                    elem.selectionEnd = pos;   
                    elem.selectionStart = pos;   
                    if( text === undefined ) {   
                        return pos;   
                    }   
                }   
            }   
            if( text !== undefined ) {   
                var len = elem.value.length;   
                if(len<1 || pos<0) {   
                    pos = 0;   
                } else if( pos>len ) {   
                    pos = len;   
                }   
                var text1 = elem.value.substr(0,pos);   
                var text2 = elem.value.substr(pos);   
                pos += text.length;   
                elem.value = text1+text+text2;   
                return this.position(pos);   
            }   
        }else{   
            var index = 0;   
            if( typeof pos === "number") {   
                index += pos;   
            }   
            return index;   
        }   
    }   
})

좋은 웹페이지 즐겨찾기