兼容所有浏览器,兼容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)" />