社区文档构建进行中,欢迎编辑。社区答疑群(非官方):717421103,点点小课堂(腾讯会议):5696651544

全站通知:

Copy

阅读

    

2024-02-23更新

    

最新编辑:Daikou

阅读:

  

更新日期:2024-02-23

  

最新编辑:Daikou

来自WIKI实验室WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索
页面贡献者 :
congeal_plume
Daikou

此模板为一键安装导入,不建议手动修改。

简单说明

版本:1.0

说明:点击复制

使用方式

引入Widget:copy在需要复制的元素类样式加入“copy-text”标签

{{#Widget:copy}}
<div class="copy-text" style="width: 50%; height:300px;">我是要复制的内容</div>

我是要复制的内容

作者

congeal_plume


依赖页面

<script type="text/javascript"> /* https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.4.0/clipboard.min.js */ !function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.Clipboard=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){var d=a("closest"),e=a("component-event"),f=["focus","blur"];c.bind=function(a,b,c,g,h){return-1!==f.indexOf(c)&&(h=!0),e.bind(a,c,function(c){var e=c.target||c.srcElement;c.delegateTarget=d(e,b,!0,a),c.delegateTarget&&g.call(a,c)},h)},c.unbind=function(a,b,c,d){-1!==f.indexOf(b)&&(d=!0),e.unbind(a,b,c,d)}},{closest:2,"component-event":4}],2:[function(a,b,c){var d=a("matches-selector");b.exports=function(a,b,c){for(var e=c?a:a.parentNode;e&&e!==document;){if(d(e,b))return e;e=e.parentNode}}},{"matches-selector":3}],3:[function(a,b,c){function d(a,b){if(f)return f.call(a,b);for(var c=a.parentNode.querySelectorAll(b),d=0;d<c.length;++d)if(c[d]==a)return!0;return!1}var e=Element.prototype,f=e.matchesSelector||e.webkitMatchesSelector||e.mozMatchesSelector||e.msMatchesSelector||e.oMatchesSelector;b.exports=d},{}],4:[function(a,b,c){var d=window.addEventListener?"addEventListener":"attachEvent",e=window.removeEventListener?"removeEventListener":"detachEvent",f="addEventListener"!==d?"on":"";c.bind=function(a,b,c,e){return a[d](f+b,c,e||!1),c},c.unbind=function(a,b,c,d){return a[e](f+b,c,d||!1),c}},{}],5:[function(a,b,c){function d(){}d.prototype={on:function(a,b,c){var d=this.e||(this.e={});return(d[a]||(d[a]=[])).push({fn:b,ctx:c}),this},once:function(a,b,c){var d=this,e=function(){d.off(a,e),b.apply(c,arguments)};return this.on(a,e,c)},emit:function(a){var b=[].slice.call(arguments,1),c=((this.e||(this.e={}))[a]||[]).slice(),d=0,e=c.length;for(d;e>d;d++)c[d].fn.apply(c[d].ctx,b);return this},off:function(a,b){var c=this.e||(this.e={}),d=c[a],e=[];if(d&&b)for(var f=0,g=d.length;g>f;f++)d[f].fn!==b&&e.push(d[f]);return e.length?c[a]=e:delete c[a],this}},b.exports=d},{}],6:[function(a,b,c){"use strict";function d(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}c.__esModule=!0;var e=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),f=function(){function a(b){d(this,a),this.resolveOptions(b),this.initSelection()}return a.prototype.resolveOptions=function(){var a=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];this.action=a.action,this.emitter=a.emitter,this.target=a.target,this.text=a.text,this.trigger=a.trigger,this.selectedText=""},a.prototype.initSelection=function(){if(this.text&&this.target)throw new Error('Multiple attributes declared, use either "target" or "text"');if(this.text)this.selectFake();else{if(!this.target)throw new Error('Missing required attributes, use either "target" or "text"');this.selectTarget()}},a.prototype.selectFake=function(){var a=this;this.removeFake(),this.fakeHandler=document.body.addEventListener("click",function(){return a.removeFake()}),this.fakeElem=document.createElement("textarea"),this.fakeElem.style.position="absolute",this.fakeElem.style.left="-9999px",this.fakeElem.style.top=document.body.scrollTop+"px",this.fakeElem.setAttribute("readonly",""),this.fakeElem.value=this.text,this.selectedText=this.text,document.body.appendChild(this.fakeElem),this.fakeElem.select(),this.copyText()},a.prototype.removeFake=function(){this.fakeHandler&&(document.body.removeEventListener("click"),this.fakeHandler=null),this.fakeElem&&(document.body.removeChild(this.fakeElem),this.fakeElem=null)},a.prototype.selectTarget=function(){if("INPUT"===this.target.nodeName||"TEXTAREA"===this.target.nodeName)this.target.select(),this.selectedText=this.target.value;else{var a=document.createRange(),b=window.getSelection();a.selectNodeContents(this.target),b.addRange(a),this.selectedText=b.toString()}this.copyText()},a.prototype.copyText=function(){var a=void 0;try{a=document.execCommand(this.action)}catch(b){a=!1}this.handleResult(a)},a.prototype.handleResult=function(a){a?this.emitter.emit("success",{action:this.action,text:this.selectedText,trigger:this.trigger,clearSelection:this.clearSelection.bind(this)}):this.emitter.emit("error",{action:this.action,trigger:this.trigger,clearSelection:this.clearSelection.bind(this)})},a.prototype.clearSelection=function(){this.target&&this.target.blur(),window.getSelection().removeAllRanges()},a.prototype.destroy=function(){this.removeFake()},e(a,[{key:"action",set:function(){var a=arguments.length<=0||void 0===arguments[0]?"copy":arguments[0];if(this._action=a,"copy"!==this._action&&"cut"!==this._action)throw new Error('Invalid "action" value, use either "copy" or "cut"')},get:function(){return this._action}},{key:"target",set:function(a){if(void 0!==a){if(!a||"object"!=typeof a||1!==a.nodeType)throw new Error('Invalid "target" value, use a valid Element');this._target=a}},get:function(){return this._target}}]),a}();c["default"]=f,b.exports=c["default"]},{}],7:[function(a,b,c){"use strict";function d(a){return a&&a.__esModule?a:{"default":a}}function e(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function f(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function, not "+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)}function g(a,b){var c="data-clipboard-"+a;if(b.hasAttribute(c))return b.getAttribute(c)}c.__esModule=!0;var h=a("./clipboard-action"),i=d(h),j=a("delegate-events"),k=d(j),l=a("tiny-emitter"),m=d(l),n=function(a){function b(c,d){e(this,b),a.call(this),this.resolveOptions(d),this.delegateClick(c)}return f(b,a),b.prototype.resolveOptions=function(){var a=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];this.action="function"==typeof a.action?a.action:this.defaultAction,this.target="function"==typeof a.target?a.target:this.defaultTarget,this.text="function"==typeof a.text?a.text:this.defaultText},b.prototype.delegateClick=function(a){var b=this;this.binding=k["default"].bind(document.body,a,"click",function(a){return b.onClick(a)})},b.prototype.undelegateClick=function(){k["default"].unbind(document.body,"click",this.binding)},b.prototype.onClick=function(a){this.clipboardAction&&(this.clipboardAction=null),this.clipboardAction=new i["default"]({action:this.action(a.delegateTarget),target:this.target(a.delegateTarget),text:this.text(a.delegateTarget),trigger:a.delegateTarget,emitter:this})},b.prototype.defaultAction=function(a){return g("action",a)},b.prototype.defaultTarget=function(a){var b=g("target",a);return b?document.querySelector(b):void 0},b.prototype.defaultText=function(a){return g("text",a)},b.prototype.destroy=function(){this.undelegateClick(),this.clipboardAction&&(this.clipboardAction.destroy(),this.clipboardAction=null)},b}(m["default"]);c["default"]=n,b.exports=c["default"]},{"./clipboard-action":6,"delegate-events":1,"tiny-emitter":5}]},{},[7])(7)}); </script> <script language="javaScript"> (window.RLQ = window.RLQ || []).push([["jquery"], function () { $(function () { console.log("点击复制加载成功");           $(".copy-text").prepend('<div class="copy-svg" title="点击复制"><svg width="1em" height="1em"viewBox="0 0 32 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M0 4C0 1.79086 1.79086 0 4 0H20C20.5304 0 21.0391 0.210714 21.4142 0.585786L31.4142 10.5858C31.7893 10.9609 32 11.4696 32 12V36C32 38.2091 30.2091 40 28 40H4C1.79086 40 0 38.2091 0 36V4ZM27.1716 12H20V4.82843L27.1716 12ZM16 4V14C16 15.1046 16.8954 16 18 16H28V36H4V4L16 4ZM8 22C8 20.8954 8.89543 20 10 20H22C23.1046 20 24 20.8954 24 22C24 23.1046 23.1046 24 22 24H10C8.89543 24 8 23.1046 8 22ZM8 30C8 28.8954 8.89543 28 10 28H22C23.1046 28 24 28.8954 24 30C24 31.1046 23.1046 32 22 32H10C8.89543 32 8 31.1046 8 30Z" fill="currentColor" /></svg></div>') $(".copy-svg").click(function (e) { $(this) var content = $(this).parent(".copy-text").text() var clipboard = new Clipboard('.copy-svg', { text: function () { return content; } }); clipboard.on('success', function (e) { $("body").append("<div class='copy-hint'><i><svg width='1em' height='1em' viewBox='0 0 40 40' fill='none' xmlns='http://www.w3.org/2000/svg'><path fill-rule='evenodd' clip-rule='evenodd' d='M20 4C11.1634 4 4 11.1634 4 20C4 28.8366 11.1634 36 20 36C28.8366 36 36 28.8366 36 20C36 11.1634 28.8366 4 20 4ZM0 20C0 8.9543 8.9543 0 20 0C31.0457 0 40 8.9543 40 20C40 31.0457 31.0457 40 20 40C8.9543 40 0 31.0457 0 20ZM29.3287 13.5052C30.1543 14.239 30.2287 15.5032 29.4948 16.3287L18.8282 28.3287C18.4486 28.7557 17.9046 29 17.3333 29C16.7621 29 16.2181 28.7557 15.8385 28.3287L10.5052 22.3287C9.77135 21.5032 9.84571 20.239 10.6713 19.5052C11.4968 18.7713 12.761 18.8457 13.4948 19.6713L17.3333 23.9896L26.5052 13.6713C27.239 12.8457 28.5032 12.7713 29.3287 13.5052Z' fill='#42c02e'/></svg> 复制成功!</i></div> ") setTimeout('$(".copy-hint").remove()', 1500) }); clipboard.on('error', function (e) {                   $("body").append('<div class="copy-hint"><i><svg width="1em" height="1em" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M20 4C11.1634 4 4 11.1634 4 20C4 28.8366 11.1634 36 20 36C28.8366 36 36 28.8366 36 20C36 11.1634 28.8366 4 20 4ZM0 20C0 8.9543 8.9543 0 20 0C31.0457 0 40 8.9543 40 20C40 31.0457 31.0457 40 20 40C8.9543 40 0 31.0457 0 20Z" fill="#d53236"/><path fill-rule="evenodd" clip-rule="evenodd" d="M20 24C18.8954 24 18 23.1046 18 22L18 10C18 8.89543 18.8954 8 20 8C21.1046 8 22 8.89543 22 10L22 22C22 23.1046 21.1046 24 20 24Z" fill="#d53236"/><path d="M17 29C17 27.3431 18.3431 26 20 26C21.6569 26 23 27.3431 23 29C23 30.6569 21.6569 32 20 32C18.3431 32 17 30.6569 17 29Z" fill="#d53236"/></svg> 复制失败!</i></div>') setTimeout('$(".copy-hint").remove()', 1500) }); }) }) } ]) </script> <style> .copy-hint { width: 100%; display: flex; justify-content: center; transition: 0.4s; } .copy-hint>i { position: fixed; top: 20%; font-size: 18px; font-weight: bold; padding: 10px 40px; border-radius: 10px; background-color: #fff; box-shadow: 2px 2px 14px -2px #bbbbbb; display: flex; align-items: center; z-index: 1000; } .copy-text { position: relative; } .copy-svg { color: #fff; background: #3c3c3c; box-sizing: content-box; padding: 6px 10px; border-radius: 10px; position: absolute; top: 10px; right: 20px; opacity: 0; transition: 0.4s ease-in-out; cursor: pointer; } .copy-text:hover>.copy-svg { opacity: 1; transition: 0.4s ease-in-out; } </style>

更新日志

1.0