处理默认右键复制,添加额外内容
<div id="wrap">这是复制的复制内容</div>
<script>
var wrap = document.getElementById('wrap')
wrap.oncopy = function (event) {
event.preventDefault()
const selection = document.getSelection()
var selectContent = selection.toString()
var dealContent =
selectContent +
'转载请联系作者,内容地址:xxxxx'
event.clipboardData.setData('text/plain', dealContent)
}
</script>
方法二
<div id="copy-text">要复制的内容</div>
<button onclick="copyText()">复制按钮</button>
<script>
const isClipboardApiSupported = () => (navigator && 'clipboard' in navigator)
console.log(isClipboardApiSupported(),navigator)
function copyText() {
const dom = document.getElementById('copy-text');
copy(dom.innerText);
}
async function copy (value) {
if(isClipboardApiSupported()) {
await navigator.clipboard.writeText(value)
alert('复制成功')
}
}
</script>