JS实现对文本进行复制

function copyContent() {
        // 由于input元素在页面上渲染后具有选中的功能,所以这里创建input的方式实现点击复制的功能
        let inputElement = document.createElement('input');
        var content = $(".contentData").val();
        inputElement.type = 'text';
        inputElement.value = content;
        document.body.appendChild(inputElement);
        // 选择增加的input元素
        inputElement.select();
        if (document.execCommand('Copy', 'false', null)) {
            // 如果复制成功
            console.log('点击内容已复制');
        }
        // 复制成功之后删除增加的这个input元素
        document.body.removeChild(inputElement);
    }
原文地址:https://www.cnblogs.com/T8888/p/14680429.html