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

Created on 2 Oct 2019  ·  2Comments  ·  Source: haizlin/fe-interview

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

js

Most helpful comment

核心代码应该就是

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

All 2 comments

核心代码应该就是

obj.select();//通过选中对象再执行复制命令
document.execCommand("Copy")
  • @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在控制台,不然不生效
Was this page helpful?
0 / 5 - 0 ratings