copy whole html table to clipboard javascript
我已经写了JavaScript来选择表格,但是现在我想在单击按钮后自动将其复制。请帮助我。我的javascript就是这样。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function selectElementContents(el) { var body = document.body, range, sel; if (document.createRange && window.getSelection) { range = document.createRange(); sel = window.getSelection(); sel.removeAllRanges(); try { range.selectNodeContents(el); sel.addRange(range); document.execCommand('Copy'); } catch (e) { range.selectNode(el); sel.addRange(range); document.execCommand('Copy'); } } else if (body.createTextRange) { range = body.createTextRange(); range.moveToElementText(el); range.select(); range.execCommand('Copy'); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function selectElementContents(el) { var body = document.body, range, sel; if (document.createRange && window.getSelection) { range = document.createRange(); sel = window.getSelection(); sel.removeAllRanges(); try { range.selectNodeContents(el); sel.addRange(range); } catch (e) { range.selectNode(el); sel.addRange(range); } } else if (body.createTextRange) { range = body.createTextRange(); range.moveToElementText(el); range.select(); } document.execCommand("Copy");} |
尝试一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <script type="text/javascript"> function copytable(el) { var urlField = document.getElementById(el) var range = document.createRange() range.selectNode(urlField) window.getSelection().addRange(range) document.execCommand('copy') } <input type=button value="Copy to Clipboard" onClick="copytable('stats')"> <table id=stats> <tr> <td>hello</td> </tr> </table> |
更新
请改用此代码。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function selectElementContents(el) { var body = document.body, range, sel; if (document.createRange && window.getSelection) { range = document.createRange(); sel = window.getSelection(); sel.removeAllRanges(); try { range.selectNodeContents(el); sel.addRange(range); } catch (e) { range.selectNode(el); sel.addRange(range); } } else if (body.createTextRange) { range = body.createTextRange(); range.moveToElementText(el); range.select(); document.execCommand("copy"); } } |
1 2 3 4 5 6 7 8 9 10 11 | <input type="button" value="select table" onclick="selectElementContents( document.getElementById('table') );"> <table id="table"> <thead> <tr><th>Heading</th><th>Heading</th></tr> </thead> <tbody> <tr><td>cell</td><td>cell</td></tr> </tbody> </table> |
-
这对我有用,它不仅限于表,它甚至可以选择ID中指定的Node内的所有元素并将其复制到剪贴板。
-
我已经在Mac Chrome和Windows chrome中进行了测试。
-
用例:复制签名生成器基于JS创建的签名
演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <p id="company_name" style="margin-top: 4px; margin-bottom: 0px; color: #522e64; font-weight: 700; font-size: 16px; letter-spacing: 1px; border-bottom: solid 2px #522e64; width:250px; padding-bottom: 3px;">The Company Name</p> <p style="margin-top: 4px; margin-bottom: 0px; color: #00706a; font-weight: 700; font-size: 15px;"> <span id="first_name_output_2"></span>Firstname<span id="last_name_output_2"> Lastname</span><span id="designation_output_2" style="color: #000000; font-weight: 500; font-size: 15px;">Designation</span></p> <p style="margin-top: 0px; margin-bottom: 0px; color: #625469; font-weight: normal; font-size: 15px; letter-spacing: 0.6px;">youreamil@xyz.com<span id="email_output_2"></span></p> <input type="button" onclick="selectElementContents( document.getElementById('signature_container') );" value="Copy to Clipboard"> function selectElementContents(el) { var body = document.body, range, sel; if (document.createRange && window.getSelection) { range = document.createRange(); sel = window.getSelection(); sel.removeAllRanges(); range.selectNodeContents(el); sel.addRange(range); } document.execCommand("Copy"); } |
以前的脚本对我不起作用,因为.execCommand(" Copy ")没有触发。通过将其附加到文档本身,并将其移至条件文件之外,我能够使其正常工作:
我认为此功能更强大:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function selectElementContents(el) { var body = document.body, range, sel; if (document.createRange && window.getSelection) { range = document.createRange(); sel = window.getSelection(); sel.removeAllRanges(); try { range.selectNodeContents(el); sel.addRange(range); } catch (e) { range.selectNode(el); sel.addRange(range); } } else if (body.createTextRange) { range = body.createTextRange(); range.moveToElementText(el); range.select(); } document.execCommand("Copy"); } |
通过使用称为
更加容易。
有关更多信息,请检查:https://webdesign.tutsplus.com/tutorials/copy-to-clipboard-made-easy-with-clipboardjs--cms-25086
1 2 3 4 5 6 7 8 9 10 11 12 | <script src="//cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.4.0/clipboard.min.js"> (function(){ new Clipboard('#copy-table-button'); })(); <button class="btn" id="copy-table-button" data-clipboard-target="#table_results">Copy</button> <table id='table_results' > </table> |
如果需要将表中的所有数据复制到剪贴板,则可以使用此自定义脚本。
html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | <button class='btnclipboard' data-source='tableStudents'> Copy table </button> <table id="tableStudents"> <thead> <th> user_id </th> <th> Name </th> </thead> <tbody> <tr> <td> 123 </td> <td> Proba </td> </tr> <tbody> </table> $('.btnclipboard').click(function(e) { e.preventDefault(); copyTableToClipboard($(this).data('source')); }); function copyTableToClipboard() { var clipboard=new Clipboard('.btnclipboard',{ text: function(trigger) { var txt=[]; var headColumns = []; $("#tableStudents th").each(function(){ var textColumn = $(this).text(); if(textColumn == null || textColumn =="") { textColumn = $(this).attr('title'); } if(textColumn == undefined || textColumn == null) { textColumn =""; } headColumns.push(textColumn); // console.log($(this).text()); }); console.log('headColumns', headColumns); var head=headColumns; txt.push(head.join("\\t")); var rowColumns=[]; $("#tableStudents tr").each(function(){ var row=[]; $(this).find('td').each(function(){ var textColumn = $(this).text(); if($(this).find("i").hasClass('fa-check')){ textColumn ="1"; } // if(textColumn =="") { // // textColumn = $(this).attr('title'); // textColumn =""; // } // if(textColumn != null) { row.push(textColumn); // } //console.log($(this).text()); }); if(row.length > 0) { rowColumns.push(row); txt.push(row.join("\\t")); } }); console.log('rowColumns', rowColumns); return txt.join("\ "); } }); clipboard.on('success', function(e) { console.info('Action:', e.action); e.clearSelection(); if (Notification.permission =="granted") { var notification = new Notification('Data copied to clipboard', { icon: '../dist/img/favicon.png', body:"You can now paste (Ctrl+v) into your favorite spreadsheet editor !" }); }else{ console.warn(Notification.permission); } }); clipboard.on('error', function(e) { console.error('Action:', e.action); console.error('Trigger:', e.trigger); }); } |
单击按钮后,应复制表格数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function selectElementContents(el) { var body = document.body, range, sel; if (document.createRange && window.getSelection) { range = document.createRange(); sel = window.getSelection(); sel.removeAllRanges(); try { range.selectNodeContents(el); sel.addRange(range); } catch (e) { range.selectNode(el); sel.addRange(range); } } else if (body.createTextRange) { range = body.createTextRange(); range.moveToElementText(el); range.select(); } CopiedTxt = document.selection.createRange(); CopiedTxt.execCommand("Copy");} |