1. 程式人生 > >input有值的情況下,第一次點選,讓游標停在最後面

input有值的情況下,第一次點選,讓游標停在最後面

參考下面做法即可:已測試通過

$(this).find(".inp-parking-space").focus();
        
moveEnd($(this).find(".inp-parking-space").get(0));//移動游標至末尾,且切換selection的位置

//移動游標到input最後面
    function moveEnd(obj) {
        obj.focus();
        var len = obj.value.length;
        if (document.selection) {
            var sel = obj.createTextRange();
            sel.moveStart('character', len); //設定開頭的位置
            sel.collapse();
            sel.select();
        } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
            obj.selectionStart = obj.selectionEnd = len;
        }
    }