美文网首页
JavaScript实现点击一段文字直接进行复制

JavaScript实现点击一段文字直接进行复制

作者: wxyzcctn | 来源:发表于2020-12-14 16:31 被阅读0次
// 点文字进行复制
function(content) {
    // 由于input元素在页面上渲染后具有选中的功能,所以这里创建input的方式实现点击复制的功能
    let inputElement = document.createElement('input');
    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);
};

相关文章

网友评论

      本文标题:JavaScript实现点击一段文字直接进行复制

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