<div id="text">被复制的文字</div>
<style type="text/css">
#input {position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;}
</style>
<textarea id="input">已经被隐藏</textarea>
<div onclick="copyText()">点击复制</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>
function copyToClip() {
var aux = document.createElement("input");
aux.setAttribute("value",“被复制的文字");
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
alert("复制成功");
}
<div onclick="copyToClip()">点击复制</div>
function copy() {
var cText = document.location.toString();
var aux = document.createElement("input");
aux.setAttribute("value", “被复制的文字");
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
alert("复制成功");
}
<div onclick="copy()">点击复制</div>
本文链接:https://www.dnwfb.com/1543.html,转载请注明出处。<input id="test" value="被复制的文字" style="display:none;"/>
<div onclick="textCopy(document.getElementById('test').value)">一键复制</div>
<script>
var textCopy=function (data) {
var f=document.createElement("form");
f.id="copy-"+Date.parse(new Date());
f.onsubmit=function(){return false};
f.style="opacity: 0;height: 1px;width: 1px;overflow: hidden;position:fixed;top: -1;left: -1;z-index: -1;"
f.innerHTML=`<button onclick="story.select();document.execCommand('Copy');"></button>
<textarea name="story">${data}</textarea>`;
document.body.appendChild(f);
document.querySelector(`#${f.id}>button`).click();
document.body.removeChild(document.getElementById(f.id));
alert("复制成功");
}
</script>
评论0