获取文本域光标位置,插入文本

                                     超级超级好用
//获取光标位置
//textDom为文本域的对象,value为将要塞入的值
function insertAfterText(textDom, value) {
    // 光标后插入文本
    var selectRange;
    if (document.selection) {
        // IE Support
        textDom.focus();
        selectRange = document.selection.createRange();
        selectRange.text = value;
        textDom.focus();
    } else if (textDom.selectionStart || textDom.selectionStart == '0') {
        // Firefox support
        var startPos = textDom.selectionStart;
        var endPos = textDom.selectionEnd;
        var scrollTop = textDom.scrollTop;
        textDom.value = textDom.value.substring(0, startPos) + value
            + textDom.value.substring(endPos, textDom.value.length);
        textDom.focus();
        textDom.selectionStart = startPos + value.length;
        textDom.selectionEnd = startPos + value.length;
        textDom.scrollTop = scrollTop;
    } else {
        textDom.value += value;
        textDom.focus();
    }
}
posted @ 2019-09-02 16:13  君主-bye  阅读(466)  评论(0编辑  收藏  举报