js复制功能
<div id="cardList"> <div class="btn" onClick="copy(111)">点击我,复制我</div> </div> <script type="text/javascript"> function copy(str){ var save = function (e){ e.clipboardData.setData('text/plain',str); e.preventDefault(); } document.addEventListener('copy',save); document.execCommand("copy"); alert("复制成功"); } </script>
<textarea cols="20" rows="10" id="biao1">用户定义的代码区域</textarea> <input type="button" onClick="copyUrl2()" value="点击复制代码" /> <script type="text/javascript"> function copyUrl2() { var Url2=document.getElementById("biao1"); Url2.select(); // 选择对象 document.execCommand("Copy"); // 执行浏览器复制命令 alert("已复制好,可贴粘。"); } </script>
<div cols="20" id="biao1">12345678dddd</div> <input type="button" onClick="copyUrl2()" value="点击复制代码" /> <script type="text/javascript"> function copyUrl2(){ var Url2=document.getElementById("biao1").innerText; var oInput = document.createElement('input'); oInput.value = Url2; document.body.appendChild(oInput); oInput.select(); document.execCommand("Copy"); oInput.style.display='none'; alert('复制成功'); } </script>
一分耕耘,一分收获