开发者

Closure not working in ZeroClipboard

I have the following JS code for ZeroClipBoard :

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');

        },

Above oncomplete is oneofmy function which is called on some action . I get item from it which is html element. Now in the above code :

    开发者_StackOverflow中文版    var text= $(item).html();//Not working when I hover the clip
       //var text= 'Hello';// This is working when I hover the clip

If I comment the first line and uncomment the second line the clip is working and text is getting copied to clipboard . But I have to use the value of that html element while copying the text . So how should I go with this ? I am getting the value of control at this point

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

But when the hover function is called it is lost. I was thinking that it will be preserved via Closure. Am I missing something ? I am not able to get the value of text at this line :

clip.setText(text);

I am not able to access any variable from outside when I am inside clip.addEventListener('mouseOver', function(client) { clip.setText(text); })


The value won't be preserved in the function call, you need to use a $.proxy instead:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜