中文版

在WordPress主题中添加一个复制按钮

发布时间:2023-12-15  浏览次数:287次

有时可能需要在WordPress主题中添加一个复制按钮,方便用户复制链接或验证码之类的,这里提供一段简单的复制代码,供参考。

html代码:

准备复制的内容

准备复制的链接
<button class="copy-btn" onclick="copyToClipboard()">复制</button>

js代码:

<script>
function copyToClipboard() {
	const quote = document.querySelector('.quoteText').textContent;
	const sourceLink = document.querySelector('.sourceLink').getAttribute('href');
	const copiedText = '文字: '   quote   '\n链接: '   sourceLink;
 
	const tempTextArea = document.createElement('textarea');
	tempTextArea.value = copiedText;
	document.body.appendChild(tempTextArea);
	tempTextArea.select();
	document.execCommand('copy');
	document.body.removeChild(tempTextArea);
 
	alert('已复制!');
}
</script>

代码实现同时复制一段文字和文字的超链接。

当前系统时间:2026-01-15 10:52:00(UTC+8) 注册人权利与责任注册人教育信息隐私政策

© 2026 重庆零玖云网络科技有限公司 版权所有,保留所有权利