复制文本内容

212 阅读1分钟

点击按钮复制文本内容,不依赖任何插件

function copyTextFn(text) {
    var input = document.createElement("input"); 
    input.value = text; 
    document.body.appendChild(input); 

    try {
        input.select(); 
        document.execCommand("Copy"); 
    } catch (error) {
        // 处理复制操作可能出现的错误
        console.error('复制操作失败:', error);
    }

    document.body.removeChild(input); 
    console.log("文本 复制成功");
}