点击按钮拷贝复制元素内文本代码实例
分类:实例代码
分享一段代码实例,它实现了点击按钮拷贝指定元素内文本的功能。
代码实例如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.pipipi.net/" /> <title>前端教程网</title> <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'); } window.onload = function () { var obt = document.getElementById("copy"); obt.addEventListener('click', copyArticle, false); } </script> </head> <body> <article id="article">前端教程网欢迎您,本站的url地址是www.pipipi.net</article> <input type="button" value="点击复制" id="copy"/> </body> </html>
点击按钮拷贝复制元素内文本代码实例,这样的场景在实际项目中还是用的比较多的,关于点击按钮拷贝复制元素内文本代码实例就介绍到这了。
点击按钮拷贝复制元素内文本代码实例属于前端实例代码,有关更多实例代码大家可以查看。