I have an anchor element with an email address inside of it, and I want it to be an anchor element so it's clear you can click on it when the cursor changes.
I googled around a bit and found a solution on another Stack Overflow question, but the way it works it adds an event listener to 'copy', which will mean that after you've clicked on it once, attempting to manually copy anything else will result in copying the text of the anchor element again.
clickToCopy.on('click', function(event) {
var copyText = $(this).text();
document.addEventListener('copy', function copyWorkaround(e) {
console.log(e)
e.clipboardData.setData('text/plain', copyText);
e.preventDefault();
}, true);
console.log(document.execCommand('copy'))
event.preventDefault()
})
This is the code. I can't figure out how to remove the event listener afterwards, or how to do it without adding the second event listener in the first place.
question from:
https://stackoverflow.com/questions/65909408/copy-text-of-an-anchor-element 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…