js 点击按钮复制文本

点击按钮复制文本(这里取用点击事件触发copy方法,加载页面直接调用会copy失败)
function copy() {
        const input = document.createElement('input');
        document.body.appendChild(input);
  //“啦啦啦”是要copy的内容,自己可以去设置
        input.setAttribute('value', '啦啦啦');
        input.select();
        if (document.execCommand('copy')) {
            document.execCommand('copy');
            console.log('复制成功');
        }else{
            console.log('复制失败');
        }
        document.body.removeChild(input);
    },

 

posted @ 2020-05-22 14:38  男孩亮亮  阅读(516)  评论(0编辑  收藏  举报