jq 复制文字到剪贴板

今天给大家分享两种实现复制文字到剪贴板的简单方法。一、使用execCommand方法用法:document.execCommand(sCommand[,交互方式, 动态参数])&nbsp;sCommand:为命令参数,如复制copy,打开open,全选selectAll等交互方式:两种可选参数true,false。true为显示对话框,false为不显示对话框。可不写动态参数:一般为可用值或属性值,可不写例如:<span id="daima">11111</span><input type="button" id="Copy" value="点击复制代码" /><script type="text/javascript">&nbsp; &nbsp; $("#Copy").click(function() {&nbsp; &nbsp; &nbsp; &nbsp; var copyText = $("#daima");//获取对象&nbsp; &nbsp; &nbsp; &nbsp; copyText .select();//选择&nbsp; &nbsp; &nbsp; &nbsp; document.execCommand("Copy");//执行复制&nbsp;    alert("复制成功!");&nbsp; &nbsp; })</script>二、clipboard.js插件例如:<p data-clipboard-text="https://www.itao520.cn"&nbsp; class="btn">前世今生</p>下载clipboard.min.js,并在页面中引用<!--&nbsp;clipboard&nbsp;Script&nbsp;-->&nbsp;&nbsp;<script src="dist/clipboard.min.js"></script>在script中实例化语句var clipboard = new Clipboard(&#39;.btn&#39;);clipboard.on(&#39;success&#39;, function(e) {&nbsp; &nbsp; console.log(e.text);&nbsp; //&nbsp;复制的文字&nbsp;});