JS:复制内容到剪贴板(无插件,兼容所有浏览器)

<button onclick="copyToClip('内容')"> Copy </button>
/**
 * 复制内容到粘贴板
 * content : 需要复制的内容
 * message : 复制完后的提示,不传则默认提示"复制成功"
 */
function copyToClip(content, message) {
    var aux = document.createElement("input"); 
    aux.setAttribute("value", content); 
    document.body.appendChild(aux); 
    aux.select();
    document.execCommand("copy"); 
    document.body.removeChild(aux);
    if (message == null) {
        alert("复制成功");
    } else{
        alert(message);
    }
}

 

posted @ 2020-06-30 17:28  <_/>  阅读(1198)  评论(0编辑  收藏  举报