方法一:
var input = document.createElement('input');
input.setAttribute('id', 'input_for_copyText');
input.value = '需要复制的文本';
document.getElementsByTagName('body')[0].appendChild(input);
document.getElementById('input_for_copyText').select();
var boole = document.execCommand('copy');
document.getElementById('input_for_copyText').remove();
console.log(boole);
if (boole) Toastr.info("复制成功");
else Toastr.error("复制失败");
方法二:
var copyTarget = document.querySelector('.btn-copy');
var range = document.createRange();
range.selectNode(copyTarget);
window.getSelection().addRange(range);
var boole = document.execCommand('copy');
if (boole) Toastr.info("复制成功");
else Toastr.error("复制失败");
发表评论 取消回复