console
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>监听指定div的复制</title>
</head>
<body>
<div id="div1" data-faq-id="a123b-895">
只监听这个div中的复制
<div>
子元素监听
<div>
孙子元素
</div>
</div>
</div>
<div id="div2">
不监听这个div的复制
</div>
<script>
const div1 = document.getElementById('div1');
div1.addEventListener('load', () => {
console.log('loaded')
div1.addEventListener('copy', e => {
const selection = window.getSelection().toString();
const copiedElement = e.target;
const faqId = copiedElement.closest('[data-faq-id]');
if (faqId) {
console.log(`你复制了div1中的内容:${selection}, id:${faqId.dataset.faqId}`);
}
});
});
</script>
</body>
</html>