JS复制
* 复制
1、复制文本
1、点击即可复制:
<!--处理时间--> @{var sTrclick = string.Format("onclick=copydata(this,'{0}','{1}')", item.f_id, item.f_accounts + "-" + Base.SiteNumber); } <td @sTrclick> <span class='red'>@item.f_spendTime</span> </td> <script> //组装特定数据到剪切板 function copydata(me, f_id, f_account,f_money) { //银行存款:10837119 P62308-21 彰化珮其-A11- (約), 20000 var bObj = $(me).closest("tr").find("td[name='Fbook']"); var f_book = bObj.find("select option:selected").text(); var f_book1 = bObj.find("span").text() var f_money = $(me).closest("tr").find("td[name='fMoney'] font").text(); var _vTxt = f_id + " " + f_account + " " + (f_book || f_book1) + "," + f_money.replace(/,/g, ''); copyToClipboard(_vTxt); layer.tips(jsonCode["Font_fuzhichenggong"], $(me)) } </script>
common.js通用复制方法
//将特定内容添加到剪切板里 function copyToClipboard(s) { if (window.clipboardData) { window.clipboardData.setData('text', s); } else { (function (s) { document.oncopy = function (e) { e.clipboardData.setData('text', s); e.preventDefault(); document.oncopy = null; } })(s); document.execCommand('Copy'); } }
2、点击复制图片即可复制:
<td align="left" onselectstart='return false' onselect='document.selection.empty()' colspan="2"> <span class="bolder bigger-140" id="ltlAccount" style="color:'gray'">@Model.f_RemittanceAccount</span> <img src="~/Content/Images/@Base.SiteYx/btn_copy2.png" display="inline" width="37" height="19" onclick="oCopy()" data-clipboard-action="copy" data-clipboard-target="#ltlAccount" data-myalert="ltlAccount" style="float: right; cursor: pointer;" /> </td> <script> //复制 function oCopy() { var clipboard = new Clipboard(document.querySelectorAll('img'), { text: function (trigger) { var target = trigger.getAttribute('data-clipboard-target'); if (target && target === '#ltlnotGiveMoney') { return $(target).html().replace(new RegExp(',', 'g'), "").trim(' '); } } }); // 复制内容到剪贴板成功后的操作 clipboard.on('success', function (e) { layer.tips('@LocalHelper.Lang("Font_fuzhichenggong")', e.trigger, { tips: [2, '#78BA32'], time: 1000 }); }); } </script>