在光标处插入结点或字符串

function insertNodeAtCursor(node) {

    var range, html;

    if (window.getSelection && window.getSelection().getRangeAt) {

        range = window.getSelection().getRangeAt(0);

        range.insertNode(node);

    } else if (document.selection && document.selection.createRange) {

        range = document.selection.createRange();

        html = (node.nodeType == 3) ? node.data : node.outerHTML;

        range.pasteHTML(html);

    }

}
function insertHtmlAtCursor(html) {

    var range, node;

    if (window.getSelection && window.getSelection().getRangeAt) {

        range = window.getSelection().getRangeAt(0);

        node = range.createContextualFragment(html);

        range.insertNode(node);

    } else if (document.selection && document.selection.createRange) {

        document.selection.createRange().pasteHTML(html);

    }

}

你可能感兴趣的:(字符串)