ZeroClipboard跨浏览器复制到剪切板

步骤:

1、引入js文件,记得先引入jquery

<script type="text/javascript" src="ZeroClipboard.js"></script>

2、然后配置swf文件的路径

ZeroClipboard.config( { swfPath: "http://YOURSERVER/path/ZeroClipboard.swf" } );

3、创建客户端

var client = new ZeroClipboard($(".copy-button"));

4、使用copy功能

 

client.on( "copy", function (event) {

  var clipboard = event.clipboardData;

  clipboard.setData( "text/plain", "Copy me!" );

  alert('copy over!);

  // clipboard.setData( "text/html", "<b>Copy me!</b>" );

  // clipboard.setData( "application/rtf", "{\\rtf1\\ansi\n{\\b Copy me!}}" );

});

具体使用:

<html>
   <head>
     <style type= "text/css" >
       .clip_button {
         text-align: center;
         border: 1px solid black;
         background-color:  #ccc;
         margin: 10px;
         padding: 10px;
       }
       .clip_button.zeroclipboard-is-hover { background-color:  #eee; }
       .clip_button.zeroclipboard-is-active { background-color:  #aaa; }
     </style>
   </head>
   <body>
     <script type= "text/javascript"  src= "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
     <script type= "text/javascript"  src= "ZeroClipboard.js" ></script>
 
     <div  class = "clip_button" >Copy To Clipboard</div>
     <div  class = "clip_button" >Copy This Too!</div>
 
     <script type= "text/javascript" >
       var  client =  new  ZeroClipboard( $( '.clip_button' ) );
 
       client.on(  'ready' function (event) {
         // console.log( 'movie is loaded' );
 
         client.on(  'copy' function (event) {
           event.clipboardData.setData( 'text/plain' , event.target.innerHTML);
         } );
 
         client.on(  'aftercopy' function (event) {
           console.log( 'Copied text to clipboard: '  + event.data[ 'text/plain' ]);
         } );
       } );
 
       client.on(  'error' function (event) {
         // console.log( 'ZeroClipboard error of type "' + event.name + '": ' + event.message );
         ZeroClipboard.destroy();
       } );
     </script>
   </body></html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值