原生js复制到剪切板

// 点击按钮调用复制
function copyBtn() {
    copyText($("#testbtn").val(), function () {alert('复制成功')});
}

// 复制的方法
function copyText(text, callback) { // text: 要复制的内容, callback: 回调
    var tag = document.createElement('input');
    tag.setAttribute('id', 'cp_hgz_input');
    tag.value = text;
    document.getElementsByTagName('body')[0].appendChild(tag);
    document.getElementById('cp_hgz_input').select();
    document.execCommand('copy');
    document.getElementById('cp_hgz_input').remove();
    if (callback) {
        callback(text)
    }
}

找不到看那位大佬的博客了,还有一种方法是引入一个外部的js来实现的app

相关文章
相关标签/搜索