• 微信
您当前的位置:首页  > 文章 >  几种js点击复制代码

几种js点击复制代码

作者:admin 时间:2021-09-02 阅读数:406人

<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>

<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>

声明

源码下载不顺利,比如源码在百度网盘的有时会被无故取消等,请联系QQ:46667551
本站大部分下载资源收集于网络,但除特别说明之外,基本全部亲自测试可用!
但由于某些源码的更新迭代,比如微信小程序官方接口的变动等原因,
如时间过长,可能会造成本可以使用的代码出现问题,下载前请斟酌!
本站资源仅供学习和交流使用,版权归原作者所有,请在下载后24小时之内自觉删除。
若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。

原文链接:https://www.love0312.com/1543.html,转载请注明出处