<div class="demo-area"> <button id="d_clip_button" class="my_clip_button" title="Click me to copy to clipboard." data-clipboard-target="fe_text" data-clipboard-text="Default clipboard text from attribute"><b>Copy To Clipboard...</b></button> <h4><label for="fe_text">Change Copy Text Here</label></h4> <textarea id="fe_text" cols="50" rows="3">Copy me!</textarea> </div> <h4>Debug Console:</h4> <div id="d_debug"></div> <script type="text/javascript" src="ZeroClipboard.min.js"></script> <script language="JavaScript"> $(document).ready(function() { var clip = new ZeroClipboard($("#d_clip_button"), { moviePath: "ZeroClipboard.swf" }); clip.on('load', function (client) { debugstr("Flash movie loaded and ready."); }); clip.on('noFlash', function (client) { $(".demo-area").hide(); debugstr("Your browser has no Flash."); }); clip.on('wrongFlash', function (client, args) { $(".demo-area").hide(); debugstr("Flash 10.0.0+ is required but you are running Flash " + args.flashVersion.replace(/,/g, ".")); }); clip.on('complete', function (client, args) { debugstr("Copied text to clipboard: " + args.text); }); // jquery stuff (optional) function debugstr(text) { $("#d_debug").append($("<p>").text(text)); } }); </script>
下载地址:DEMO+全部文件
为什么下载的DEMO不能运行,点击不能复制?
答:这个必须运行在真实环境上,直接用浏览器打开是不行的,用IIS运行就可以。
使用方法:
导入 ZeroClipboard.js 文件:
<script type="text/javascript" src="ZeroClipboard.js"></script>
附上项目实际使用的代码:
$(document).ready(function() { //要点击对象 var objName="#contact_door_text"; var clip = new ZeroClipboard($(objName), { moviePath: "script/ZeroClipboard.swf" }); //提示复制成功 clip.on('complete', function (client, args) { alert("已复制到剪切板!"); }); });