<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>复制功能</title> <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script> </head> <body> <button type="button" id="button">点击复制微信</button> <div id="weixin"> 微信:789465456 </div> <!-- //首先要在html中写一个input标签,标签不能设为隐藏,可以将透明度置为0,这样就看不见了 --> <input id="hide" type="text" style='opacity: 0'> <!-- //接着就是jq代码片段,$('.ensure')是要点击的按钮然后执行复制功能 --> </body> <script type="text/javascript"> $("#button").click(function() { var html = $("#weixin").html(); $("#hide").val(html); //这里可以获取动态数据赋值给$("#hide").val() $("#hide").select(); try { var state = document.execCommand("copy"); } catch (err) { var state = false; } if (state) { alert("微信号复制成功,请去添加") } else { alert("微信号复制失败") } }) </script> </html>