其他分享
首页 > 其他分享> > insert-text-at-cursor-in-a-content-editable-div

insert-text-at-cursor-in-a-content-editable-div

作者:互联网

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;
    }
}

 

标签:insert,document,text,editable,range,selection,sel,getRangeAt
来源: https://www.cnblogs.com/fenle/p/15618377.html