光标定位到文本的末尾

 

http://stackoverflow.com/questions/6003300/how-to-place-cursor-at-end-of-text-in-textarea-when-tabbed-into/6003829#6003829

 

<textarea id="test">Some text</textarea>

function moveCaretToEnd(el) {

 if (typeof el.selectionStart == "number") {

el.selectionStart = el.selectionEnd = el.value.length;

}

else if (typeof el.createTextRange != "undefined") {

el.focus(); var range = el.createTextRange();

 range.collapse(false); range.select();

}}

var textarea = document.getElementById("test");

textarea.onfocus = function() {

moveCaretToEnd(textarea

); // Work around Chrome's little problem

window.setTimeout(function() {

moveCaretToEnd(textarea); },

1);

};

 

 

2.

//光标定位到文本末尾
function myFocus(sel, start, end) {
    if (sel.setSelectionRange) {
        sel.focus();
        sel.setSelectionRange(start, end);
    }
    else if (sel.createTextRange) {
        var range = sel.createTextRange();
        range.collapse(true);
        range.moveEnd('character', end);
        range.moveStart('character', start);
        range.select();
    }
}
function setFocus(selec) {
    var len = selec.value.length;
    myFocus(selec, len, len);
}

setFocus($("#UserAccount")[0]); //使用

你可能感兴趣的:(js,js,js)