美文网首页
js 复制链接到剪切板

js 复制链接到剪切板

作者: 水君子Z | 来源:发表于2019-02-20 15:53 被阅读0次
function copy(callback) {
  if(document.execCommand('Copy')){
    //创建input
    var inputZ = document.createElement('input');
    //添加Id,用于后续操作
    inputZ.setAttribute('id','inputCopy');
    //获取当前链接
    inputZ.value = window.location.href;
    //创建的input添加到body
    document.body.appendChild(inputZ);
    //选中input中的值
    document.getElementById('inputCopy').select();
    //把值复制下来
    document.execCommand('Copy')
    alert('複製成功');
    //删除添加的input
    document.body.removeChild(inputZ);
    // 成功回調1
    callback(1);
  }else{
    // 失敗回調2
    callback(2);
  }
}

相关文章

网友评论

      本文标题:js 复制链接到剪切板

      本文链接:https://www.haomeiwen.com/subject/mnshyqtx.html