可以使用浏览器的复制api: document.execCommand("copy"); 完整代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
.wrapper {
position: relative;
}
#input {
position: absolute;
top: 0;
left: 0;
opacity: 0;
z-index: -10;
}
</style>
</head>
<body>
<div class="wrapper">
<p id="text">
待复制的内容
</p>
<textarea id="input">所有的复制都要经过我</textarea>
<buttonBfwOnclick="copyText()">复制</button>
</div>
<script type="text/javascript">
function copyText() {
var text = document.getElementById("text").innerText;
var input = document.getElementById("input");
input.value = text; // 修改文本框的内容
input.select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令
alert("复制成功");
}
</script>
</body>
</html>
网友回复
CefSharp与Electron开发桌面应用哪个更好?
在哪可以免费白嫖claude 4.5?
如何编写一个chrome插件实现多线程高速下载大文件?
cdn版本的vue在网页中出现typeerror错误无法找到错误代码位置怎么办?
pywebview能否使用webrtc远程控制共享桌面和摄像头?
pywebview6.0如何让窗体接受拖拽文件获取真实的文件路径?
如何在linux系统中同时能安装运行apk的安卓应用?
python有没有离线验证码识别ocr库?
各家的ai图生视频及文生视频的api价格谁最便宜?
openai、gemini、qwen3-vl、Doubao-Seed-1.6在ui截图视觉定位这款哪家更强更准?