js实现复制,打印功能
2017-12-28 14:18
246 查看
js实现复制
js实现打印
function copyToClipboard(elem) { // create hidden text element, if it doesn't already exist var targetId = "_hiddenCopyText_"; var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA"; var origSelectionStart, origSelectionEnd; if (isInput) { // can just use the original source element for the selection and copy target = elem; origSelectionStart = elem.selectionStart; origSelectionEnd = elem.selectionEnd; } else { // must use a temporary form element for the selection and copy 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; } // select the content var currentFocus = document.activeElement; target.focus(); target.setSelectionRange(0, target.value.length); // copy the selection var succeed; try { succeed = document.execCommand("copy"); } catch(e) { succeed = false; } // restore original focus if (currentFocus && typeof currentFocus.focus === "function") { currentFocus.focus(); } if (isInput) { // restore prior selection elem.setSelectionRange(origSelectionStart, origSelectionEnd); } else { // clear temporary content target.textContent = ""; } return succeed; }
js实现打印
printToClipboard(elem) { const newWindow = window.open(`打印窗口`, `_blank`); const docStr = elem.innerHTML; newWindow.document.write(docStr); newWindow.document.close(); newWindow.print(); newWindow.close(); }
相关文章推荐
- JS+调用word打印功能实现在Webfrom客户端
- js实现复制功能
- JS 使用 window对象的print方法实现分页打印功能
- Flash+JS实现跨浏览器复制剪贴板功能
- js实现一键复制功能
- JS实现复制和粘贴功能
- js一键复制,Clipboard.js:不用Flash实现剪贴板功能的轻量级JavaScript库
- js实现打印、页面设置、打印预览功能
- JS实现打印功能
- js实现复制功能
- js实现web套打功能,支持拖动布局,支持按键移动支持打印预览,结果可保存至数据库中
- 【转】js实现复制到剪贴板功能,兼容所有浏览器
- 纯js实现复制到剪贴板功能
- js/jQuery实现复制到剪贴板功能,兼容所有浏览器
- js 实现页面的复制功能 clipboard.js
- JS 实现分页打印功能
- js中实现 复制到剪切板 功能
- js实现复制的功能
- Zero Clipboard js+swf实现的复制功能使用方法
- JS实现复制内容到剪贴板功能