【问题标题】:Closure not working in ZeroClipboard关闭在 ZeroClipboard 中不起作用
【发布时间】:2011-01-30 17:54:18
【问题描述】:

我有以下用于 ZeroClipBoard 的 JS 代码:

onComplete: function(item) {

            var text= $(item).html();//Not working when I hover the clip
           //var text= 'Hello';// This is working when I hover the clip

            var clip = new ZeroClipboard.Client();
            clip.setHandCursor(true);

            clip.addEventListener('complete', function(client, text) {
                debugstr("Copied text to clipboard: " + text );
            });

            clip.addEventListener('mouseOver', function(client) {
                clip.setText(text);
            })

            // glue specifying our button AND its container
            clip.glue('id_clip_button', 'id_clip_container');

        },

oncomplete 上面是 oneofmy 函数,它在某些动作上被调用。我从中得到项目,它是 html 元素。 现在在上面的代码中:

        var text= $(item).html();//Not working when I hover the clip
       //var text= 'Hello';// This is working when I hover the clip

如果我注释第一行并取消注释第二行,则剪辑正在工作并且文本被复制到剪贴板。但是我必须在复制文本时使用该 html 元素的值。那么我该怎么做呢?我在这一点上得到了控制的价值

var text= $(item).html();//

但是当调用悬停函数时,它会丢失。我在想它将通过 Closure 保存。我错过了什么吗?我无法在这一行获取文本的值:

clip.setText(text);

当我在内部时,我无法从外部访问任何变量 clip.addEventListener('mouseOver', function(client) { clip.setText(text); })

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    函数调用中不会保留该值,您需要使用$.proxy 代替:

            clip.addEventListener('mouseOver', $.proxy(function(client) {
                // "this" is now set to text
                clip.setText(this);
            }, text));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多