js复制内容到剪贴板,不弹出输入框

document.addEventListener('click', function bindCopy(e) {
    dom = document.createElement('textarea');
    // Prevent zooming on iOS
    dom.style.fontSize = '12pt';
    // Reset box model
    dom.style.border = '0';
    dom.style.padding = '0';
    dom.style.margin = '0';
    // Move element out of screen horizontally
    dom.style.position = 'absolute';
    dom.style['left'] = '-9999px';
    // Move element to the same position vertically
    var yPosition = window.pageYOffset || document.documentElement.scrollTop;
    dom.style.top = yPosition + 'px';

    dom.setAttribute('readonly', '');
    dom.value = '需要复制到剪贴板的内容';

    document.body.appendChild(dom);

    dom.select();
    dom.setSelectionRange(0, dom.value.length);
    document.execCommand('copy');
    document.removeEventListener('click', bindCopy);
})

复制这个行为必须由用户触发。

posted @ 2019-03-12 11:22  cococe  阅读(986)  评论(0编辑  收藏  举报