前端JS实现简易一键复制黏贴函数(可复制换行)

废话不多说, 直接上代码, 有问题下面或者私信讨论哈

定义

function copy(copyContent) {
    // 1. 创建并添加一个输入框元素(最后会销毁)
    const textareaEle = document.createElement("textarea");
    document.body.appendChild(textareaEle);
    // 2. 将需要复制的文本传入输入框, 并调用 select 方法, 选中输入框中文本
    textareaEle.value = copyContent;
	textareaEle.select();
    textareaEle.readOnly = 'readOnly';
    // 3. 调用复制选中文本的方法
    document.execCommand('copy');
    // 4. 销毁输入框
    document.body.removeChild(textareaEle);
}

使用

copy("我马上会到你的剪贴板里嘿嘿")

おすすめ

転載: blog.csdn.net/weixin_47170010/article/details/121583655