Zeroclipboard在第一次单击时未复制 [英] Zeroclipboard not copying on first Click

查看:114
本文介绍了Zeroclipboard在第一次单击时未复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码,第一次单击时不起作用,但是在第二次单击时它起作用.

I have the code and it is not working on first click, but on the second click it is working.

$("#btnCopiar").on("click",function(){
        var clipBoardObj = new ZeroClipboard($("#btnCopiar"), {
              moviePath: "../thirdparty/ZeroClipboard.swf"
        });;
        // Create your data here to copy to the clipboard and assign to a variable name data 
         var data =   "DATA IS COMING FROM SERVER OT TEXT INPUT";
                clipBoardObj.on("copy", function (event) {                  
                var clipboard = event.clipboardData;
                  clipboard.setData( "text/plain", data );
        });

    });

<button id="btnCopiar">Copiar</button>

即使我在click事件之外初始化了剪贴板,也无法正常工作

Even if I have initialized the clipboard outside the click event, it is not working

推荐答案

我想知道这是否与您编写代码的同步方式有关.

I wonder if this has to with the synchronous way you have written the code.

您的行var data = ...表示变量data正在从服务器调用中接收信息,而该信息仅在此时发生. (为了使问题更简洁和易于理解,我正在做一些关于您已删除代码的假设,尽管我对此可能有误.)数据将需要一些时间才能到达.但是,紧接在该行之后,您正在使用clipBoardObj.on("copy", function(event) {...函数中的data变量.第一次运行该功能时,data尚未到达.但是,在用户第二次单击按钮之前,将花费一些时间.发生这种情况时,可能已经有足够的时间使对服务器的第一次调用返回,并且data将具有一些数据.但是请注意,第二次运行该函数时,它将仅使用对服务器的 first 调用中的data.

Your line var data = ... implies that the variable data is receiving its information from a call to the server that only happens right at that moment. (I'm making some assumptions about code you have deleted in order to make the question more concise and understandable, though I could be wrong about that.) That data is going to take a little while to arrive. However, immediately after that line you are using the data variable in the clipBoardObj.on("copy", function(event) {... function. The first time you run that function, the data will not yet have arrived. However, some time will elapse before the user clicks the button a second time. When that happens, there may have been enough time for the first call to the server to have returned, and data will have some data. Note, however, that the second time you run that function, it will only be using the data from the first call to the server, which may or may not be acceptable.

这篇关于Zeroclipboard在第一次单击时未复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆