Javascript setCursorPosition在chrome中不工作

Javascript setCursorPosition在chrome中不工作,javascript,jquery,html,Javascript,Jquery,Html,移动插入符号的功能在Chrome中不起作用,但在Firefox中起作用 $.fn.setCursorPosition = function (pos) { console.log(pos); this.each(function (index, elem) { console.log(index); console.log(elem); if (elem.setSelectionRange)

移动插入符号的功能在Chrome中不起作用,但在Firefox中起作用

$.fn.setCursorPosition = function (pos) {
        console.log(pos);
        this.each(function (index, elem) {
            console.log(index);
            console.log(elem);
            if (elem.setSelectionRange) {
                elem.setSelectionRange(pos, pos);
            } else if (elem.createTextRange) {
                var range = elem.createTextRange();
                range.collapse(true);
                range.moveEnd('character', pos);
                range.moveStart('character', pos);
                range.select();
            }
        });
        return this;
    };
代码: var$telInput=$('.telborder input')

解决问题:

window.setTimeout(function() {
    elem.setSelectionRange(pos, pos);
}, 0);
完整代码:

$.fn.setCursorPosition = function (pos) {
    this.each(function (index, elem) {
        if (elem.setSelectionRange) {
            window.setTimeout(function() {
                elem.setSelectionRange(pos, pos);
            }, 0);
         } else if (elem.createTextRange) {
             var range = elem.createTextRange();
             range.collapse(true);
             range.moveEnd('character', pos);
             range.moveStart('character', pos);
             range.select();
         }
    });
    return this;
};
window.setTimeout(function() {
    elem.setSelectionRange(pos, pos);
}, 0);
$.fn.setCursorPosition = function (pos) {
    this.each(function (index, elem) {
        if (elem.setSelectionRange) {
            window.setTimeout(function() {
                elem.setSelectionRange(pos, pos);
            }, 0);
         } else if (elem.createTextRange) {
             var range = elem.createTextRange();
             range.collapse(true);
             range.moveEnd('character', pos);
             range.moveStart('character', pos);
             range.select();
         }
    });
    return this;
};