div contenteditable='true'去除word粘贴的样式

可编辑的div在粘贴从word复制过来的内容时,会带着格式,清楚格式的做法如下:
<div class='page3-emjoy5' contenteditable='true' style="width: 500px; height: 500px; background: #bbb" ></div>
/*word文本复制格式化*/
    function textInit(e) {
        e.preventDefault();
        var text;
        var clp = (e.originalEvent || e).clipboardData;
        if (clp === undefined || clp === null) {
            text = window.clipboardData.getData("text") || "";
            if (text !== "") {
                if (window.getSelection) {
                    var newNode = document.createElement("span");
                    newNode.innerHTML = text;
                    window.getSelection().getRangeAt(0).insertNode(newNode);
                } else {
                    document.selection.createRange().pasteHTML(text);
                }
            }
        } else {
            text = clp.getData('text/plain') || "";
            if (text !== "") {
                document.execCommand('insertText', false, text);
            }
        }
    }
    $(".page3-emjoy5").on("paste", function (e) {
        textInit(e)
    });
posted @ 2020-04-17 22:31  胡珂儿  阅读(1226)  评论(0编辑  收藏  举报