Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
haizhilin2013 opened this issue Oct 2, 2019 · 3 comments
Open

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

haizhilin2013 opened this issue Oct 2, 2019 · 3 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

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

@haizhilin2013 haizhilin2013 added the js JavaScript label Oct 2, 2019
@wwqin
Copy link

wwqin commented Oct 3, 2019

核心代码应该就是

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

@vkboo
Copy link

vkboo commented Oct 3, 2019

  • @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在控制台,不然不生效

@xiaoqiangz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

4 participants