fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[js] 第170天 用js实现一个复制粘贴的功能

Open haizhilin2013 opened this issue 6 years ago • 3 comments

第170天 用js实现一个复制粘贴的功能

haizhilin2013 avatar Oct 02 '19 20:10 haizhilin2013

核心代码应该就是

obj.select();//通过选中对象再执行复制命令
document.execCommand("Copy")

wwqin avatar Oct 03 '19 06:10 wwqin

  • 把 @wwqin 的内容实践了一下
; (function () {
    const createInput = html => {
        let inputEl = document.createElement('input');
        inputEl.setAttribute('type', 'input');
        inputEl.value = html;
        return inputEl;
    }
    var key = '¥5uA302Tea83¥';
    var inputEl = createInput(key);
    document.body.appendChild(inputEl)
    inputEl.select();
    document.execCommand('copy')
})();
  • 注意测试时不要focus在控制台,不然不生效

vkboo avatar Oct 03 '19 14:10 vkboo

var btn = document.getElementById('btn') btn.addEventListener('click', function() { input.select() document.execCommand('copy') })

xiaoqiangz avatar Aug 03 '22 06:08 xiaoqiangz