首页 > 其他分享 >Tips: How to Get the Currently Selected HTML Content in Major Browsers

Tips: How to Get the Currently Selected HTML Content in Major Browsers

时间:2023-06-29 17:23:27浏览次数:42  
标签:Major container Currently Content How Browsers Tips sel

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

Capatibility:

https://caniuse.com/selection-api

标签:Major,container,Currently,Content,How,Browsers,Tips,sel
From: https://www.cnblogs.com/vgerbot/p/17514716.html

相关文章