//复制到剪切板 function copyToClipboard(elem) { var targetId = "_hiddenCopyText_"; var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA"; var origSelectionStart, origSelectionEnd; if (isInput) { // 如果是input标签或textarea,则直接指定该节点 target = elem; origSelectionStart = elem.selectionStart; origSelectionEnd = elem.selectionEnd; } else { // 如果不是,则使用节点的textContent target = document.getElementById(targetId); if (!target) { //如果不存在,则创建一个 var target = document.createElement("textarea"); target.style.position = "absolute"; target.style.left = "-9999px"; target.style.top = "0"; target.id = targetId; document.body.appendChild(target); } target.textContent = elem.textContent; } // 聚焦目标节点,选中它的内容 var currentFocus = document.activeElement; target.focus(); target.setSelectionRange(0, target.value.length); // 进行复制操作 var succeed; try { succeed = document.execCommand("copy"); layer.open({ type: 1, title: false, closeBtn: 1, area:['440px','250px'], skin: 'layui-layer-nobg', //没有背景色 shadeClose: true, content: $("#copyTip") }); } catch(e) { succeed = false; layer.open({ type: 1, title: false, closeBtn: 1, area:['440px','250px'], skin: 'layui-layer-nobg', //没有背景色 shadeClose: true, content: $("#copyErrTip") }); } // 不再聚焦 if (currentFocus && typeof currentFocus.focus === "function") { currentFocus.focus(); } if (isInput) { // 清空临时数据 elem.setSelectionRange(origSelectionStart, origSelectionEnd); } else { // 清空临时数据 target.textContent = ""; } return succeed; } $("#copy").on('click',function(){ var copytxt=document.querySelector('#copy'); copyToClipboard(copytxt); });
前端实现复制粘贴
最新推荐文章于 2025-02-26 17:15:29 发布