浏览器复制文本的封装
我这边是用到了。但是又怕记不住下次再查,所以就存一下
function copy(id, text) {
const range = document.createRange();
range.selectNode(document.getElementById(id));
const selection = window.getSelection();
if (selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy'); alert(text);
} //id 是要复制的元素id text 是弹窗显示的内容
还有一种
function Copy(str){
var save = function(e){
e.clipboardData.setData('text/plain', str);
e.preventDefault();
}
document.addEventListener('copy', save);
document.execCommand('copy');
document.removeEventListener('copy',save);
alert('复制成功!');
}
$('#cardList').on('click', 'div.btn', function(){
Copy($(this).prev('div').find('span.code').text());
});