function keepLastIndex(obj) {
    if (window.getSelection) {//ie11 10 9 ff safari
        obj.focus(); // Resolve the problem that ff cannot locate without getting focus
        var range = window.getSelection();/ / create the range
        range.selectAllChildren(obj);//range selects all children under obj
        range.collapseToEnd();// The cursor moves to the end
    }
    else if (document.selection) {//ie10 9 8 7 6 5
        var range = document.selection.createRange();// Create a selection object
        //var range = document.body.createTextRange();
        range.moveToElementText(obj);//range locates to obj
        range.collapse(false);// The cursor moves to the end
        range.select();
    }
}
keepLastIndex(document.getElementById("div"))
Copy the code