作者:段昊辰 链接:https://zhuanlan.zhihu.com/p/23920249 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <article id="article"> <h4>公园一日游</h4> <time>2016.8.15 星期二</time> <p>今天风和日丽,我和小红去了人民公园,玩了滑梯、打雪仗、划船,真是愉快的一天啊。</p> </article> <button id="copy">复制文章</button> <textarea style="width: 500px;height: 100px;" placeholder="试一试ctrl + v"></textarea> <script> function copyArticle(event){ const range = document.createRange(); range.selectNode(document.getElementById('article')); const selection = window.getSelection(); if(selection.rangeCount > 0) selection.removeAllRanges(); selection.addRange(range); document.execCommand('copy'); } document.getElementById('copy').addEventListener('click', copyArticle, false); </script> </body> </html>
转载于:https://my.oschina.net/newgoup/blog/793991