https://stackoverflow.com/questions/2920150/insert-text-at-cursor-in-a-content-editable-div

 

function insertTextAtCaret(text) {
    var sel, range;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.deleteContents();
            range.insertNode( document.createTextNode(text) );
        }
    } else if (document.selection && document.selection.createRange) {
        document.selection.createRange().text = text;
    }
}

 

posted on 2021-11-29 10:54  jzfan  阅读(29)  评论(0编辑  收藏  举报