js 复制div内容到剪切板

//点击复制到剪切板
$("#copytocutpad").click(function(){
    
    var div = document.getElementById('sharedurl');
        if (document.body.createTextRange) {
            var range = document.body.createTextRange();
            range.moveToElementText(div);
            range.select();
        } else if (window.getSelection) {
            var selection = window.getSelection();
            var range = document.createRange();
            range.selectNodeContents(div);
            selection.removeAllRanges();
            selection.addRange(range);
            /*if(selection.setBaseAndExtent){
                selection.setBaseAndExtent(text, 0, text, 1);
            }*/
        } else {
            console.warn("none");
        }
        document.execCommand("Copy"); // 执行浏览器复制命令
 
});

 

posted @ 2020-04-10 16:05  _DC  阅读(1740)  评论(0编辑  收藏  举报