下面代码复制不了 检测bug 并且优化写出代码button id=copy-button class=refresh-button复制buttonscriptconst copyButton = documentgetElementByIdcopy-button;const responseTextarea = documentgetElementByIdchatgpt-response;copy
<p><button id="copy-button" class="refresh-button">复制</button></p>
<textarea id="chatgpt-response"></textarea>
<script>
const copyButton = document.getElementById('copy-button');
const responseTextarea = document.getElementById('chatgpt-response');
copyButton.addEventListener('click', () => {
responseTextarea.select();
document.execCommand('copy');
});
// 优化部分
// 检测浏览器是否支持 execCommand
if (!document.queryCommandSupported('copy')) {
copyButton.style.display = 'none';
}
// 添加复制成功提示
responseTextarea.addEventListener('copy', (event) => {
event.preventDefault();
if (event.clipboardData) {
event.clipboardData.setData('text/plain', responseTextarea.value);
console.log('复制成功!');
}
});
</script>
原文地址: https://www.cveoy.top/t/topic/14b 著作权归作者所有。请勿转载和采集!