方法一:

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("复制失败");

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部

Copyright © 2016-2025.By阿狸小乖 版权所有    主页

滇ICP备17010414号-1