window.getSelection返回html [duplicate]

    这个问题在这里已有答案:                           所选文字的HTML                                      1个答案                                    
已邀请:
以下内容将在所有主流浏览器中执行此操作,并且与此答案完全相同:
function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}
    

要回复问题请先登录注册