js 复制



//  单个复制
<div id=“fuzhi">复制</div>
 <p id="copyBT”>复制内容</p>
function copyArticle1(event) {
    const range = document.createRange();
    // 复制元素
    range.selectNode(document.getElementById('copyBT'));
    const selection = window.getSelection();
    if (selection.rangeCount > 0) selection.removeAllRanges();
    selection.addRange(range);
    document.execCommand('copy');
    mui.toast("复制成功!");
}
document.getElementById('fuzhi').addEventListener('click', copyArticle1, false);

// 多个复制  把需要复制的文字给按钮
$(document).on("click", ".fuzhi", function() {
    $("#copyBT2").html("");
    //          data_con 复制内容
    var data = $(this).attr("data_con");
    $("#copyBT2").html(data);
    copyArticle();
})

function copyArticle(event) {
    const range = document.createRange();
    // 复制元素
    range.selectNode(document.getElementById('copyBT2'));
    const selection = window.getSelection();
    if (selection.rangeCount > 0) selection.removeAllRanges();
    selection.addRange(range);
    document.execCommand('copy');
    console.log("复制成功!");
} 
<div id = "copyBT2" style = "opacity: 0;" > </div>


版权声明:本文为weixin_44186787原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。