fe-interview
fe-interview copied to clipboard
[js] 第170天 用js实现一个复制粘贴的功能
第170天 用js实现一个复制粘贴的功能
核心代码应该就是
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在控制台,不然不生效
var btn = document.getElementById('btn') btn.addEventListener('click', function() { input.select() document.execCommand('copy') })