JS 复制粘贴

in 前端JavaScript with 0 comment

使用 navigator Api 复制

const copyText = (content) => {
  navigator.clipboard.writeText(content)
  Toast({
    message: i18n.t('A11032'),
    position: 'bottom'
  })
}

使用 clipboard 包

https://www.npmjs.com/package/clipboard

<button class="btn" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
    Copy to clipboard
</button>

var clipboard = new ClipboardJS('.btn');
clipboard.on('success', function(e) {
    console.info('Action:', e.action);
    console.info('Text:', e.text);
    console.info('Trigger:', e.trigger);

    e.clearSelection();
});

clipboard.on('error', function(e) {
    console.error('Action:', e.action);
    console.error('Trigger:', e.trigger);
});

Comments are closed.