js 实现复制内容到剪切版 兼容浏览器和手机浏览器

    xiaoxiao2022-07-02  105

    以前做网页复制的时候用过flash插件,不过不支持手机.后来找到纯js实现非常简单分享给大家

    兼容所有浏览器,兼容pc和手机 不过不兼容ios

    <script> function copystr(str) { var oInput = document.createElement('input'); oInput.value = str; document.body.appendChild(oInput); oInput.select(); // 选择对象 document.execCommand("Copy"); // 执行浏览器复制命令 oInput.className = 'oInput'; oInput.style.display = 'none'; alert('复制成功'); } </script> <input type="text" id="testcopy" /> <input type="button" name="copy" value="copy" onclick="copystr(document.getElementById('testcopy').value)" />

    以下代码兼容 ios

    <script> function copystr(str) { var oInput = document.createElement('input'); oInput.value = str; document.body.appendChild(oInput); oInput.select(); // 选择对象 oInput.setSelectionRange(0, oInput.value.length), document.execCommand('Copy'); document.body.removeChild(oInput); alert('复制成功'); } </script> <input type="text" id="testcopy" /> <input type="button" name="copy" value="copy" onclick="copystr(document.getElementById('testcopy').value)" />
    最新回复(0)