
本文介绍如何使用javascript复制电话号码或字符串,javascript复制链接
javascript复制电话号码或字符串
html
<a href="javascript:copyFn('+86 18288888888')">86 18288888888</a>
主要js与解析
<script> function copyFn(value) { var isRTL = document.documentElement.getAttribute('dir') === 'rtl'; var input = document.createElement('textarea'); input.style.fontSize = '12pt'; // Reset box model input.style.border = '0'; input.style.padding = '0'; input.style.margin = '0'; input.style.position = 'absolute'; input.style[isRTL ? 'right' : 'left'] = '-9999px'; var yPosition = window.pageYOffset || document.documentElement.scrollTop; input.style.top = yPosition + 'px'; input.setAttribute('readonly', ''); input.value = value; document.body.appendChild(input); input.select(); input.setSelectionRange(0, input.value.length); if (document.execCommand) { document.execCommand('copy'); } } </script>
javascript复制链接
html
<input type="button" onclick="copyUrl()" value="点击复制到浏览器打开" />
主要js与解析
<script> function copyUrl() { var Url = window.location.href; var oInput = document.createElement('input'); oInput.value = Url; document.body.appendChild(oInput); oInput.select(); // 选择对象 document.execCommand("Copy"); // 执行浏览器复制命令 oInput.className = 'oInput'; oInput.style.display = 'none'; alert('复制成功'); } </script>