function executeCopy(elementId) {
const textToCopy = document.getElementById(elementId).innerText;
// 现代 API 复制
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(textToCopy).then(triggerToast);
} else {
// 兼容性兜底方案
const textArea = document.createElement(“textarea”);
textArea.value = textToCopy;
textArea.style.position = “fixed”;
textArea.style.left = “-9999px”;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand(‘copy’);
triggerToast();
} catch (err) {
console.error(‘无法复制’, err);
}
document.body.removeChild(textArea);
}
}
function triggerToast() {
const toast = document.getElementById(‘hcToastBox’);
toast.classList.add(‘show’);
setTimeout(() => {
toast.classList.remove(‘show’);
}, 2000);
}