function getSelectionHtml() {
    const 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());
        }
        return container.innerHTML;
    }
    return ''; 
}

Browser compatibility:

https://caniuse.com/selection-api

posted on 2023-06-29 17:13  y1j2x34  阅读(2)  评论(0编辑  收藏  举报