jquery 复制文本到剪切板插件(非 flash)
2018-01-23 16:53
363 查看
原创插件,转载请声明出处!!!
jquery.copy.js 内容如下:
jquery.copy.js 内容如下:
/*! * jQuery Copy Plugin * version: 1.0.0-2018.01.23 * Requires jQuery v1.5 or later * Copyright (c) 2018 Tiac * http://www.cnblogs.com/tujia/p/8336671.html */ // AMD support (function (factory) { "use strict"; if (typeof define === 'function' && define.amd) { // using AMD; register as anon module define(['jquery'], factory); } else { // no AMD; invoke directly factory( (typeof(jQuery) != 'undefined') ? jQuery : window.Zepto ); } } (function($) { "use strict"; /* Basic Usage: ----------- Html: <button type="button" class="btn-copy" data-clipboard-text="Copy Me!">Copy</button> JS: $('.btn-copy').copy(); Html: <div class="input-group"> <input type="text" class="form-control inp-link"> <span class="input-group-btn"> <button class="btn btn-primary btn-copy" type="button">Copy</button> </span> </div> JS: $('.btn-copy').copy({ copy: function(_this){ return _this.parents('div').find('.inp-link').val(); }, afterCopy: function(res){ if(res==true){ alert('Copied text to clipboard。'); }else{ alert('Copy failed!'); } } }); */ var clipboard_text = ''; function copyTextToClipboard(_this, text) { var oTa = jQuery('<textarea style="position:fixed;left:0;top:0;z-index:9999999999"></textarea>'); oTa.val(text); _this.after(oTa); oTa.select(); try { var result = document.execCommand('copy'); oTa.remove(); return result; } catch (err) { console.log(err); return false; } } $.fn.copy = function(options) { if(options===undefined) options = {}; var defaults = {}; defaults.copy = function(_this){ clipboard_text = _this.data('clipboard-text'); return clipboard_text; }; defaults.afterCopy = function(res, _this){ if(res){ console.log('Copied text to clipboard: ' + clipboard_text); }else{ console.log('Copy failed!'); } }; options = $.extend(defaults, options); this.on('click', function(){ clipboard_text = options.copy($(this)); var res = copyTextToClipboard($(this), clipboard_text); options.afterCopy(res, $(this)); }); }; }));
相关文章推荐
- jquery的zclip插件实现复制文本到剪切板
- jQuery常用插件(四)不使用flash,点击按钮将内容复制到剪切板
- ZeroClipboard js+flash 实现 复制文本到剪切板
- Jquery跨浏览器文本复制插件Zero Clipboard的使用方法
- jquery+flash实现点击复制到剪切板
- Jquery跨浏览器文本复制插件Zero Clipboard的使用方法
- jquery+flash(zclip)实现点击复制到剪切板
- 使用flash实现复制文本到剪切板
- jquery 一键复制文本到剪切板
- jQuery复制文本内容到剪贴板插件 ZeroClipboard
- 复制粘贴之插件(jquery.zclip.min.js)需要安装flash
- 将文本的内容复制到剪切板,ZeroClipboard
- JQuery复制内容到剪切板-jquery.zclip.js的使用,在公司项目中
- JQuery zClip插件实现复制页面内容到剪贴板
- jQuery实现复制到剪切板
- jQuery插件:跨浏览器复制jQuery-zclip
- 拷贝到剪切板——一个flash实现、兼容性不错的js插件ZeroClipboard
- js+flash(as3)实现复制文字内容到剪切板
- jQuery--计算选中的文本字数,并弹出一个层(插件四)