问题描述
我为ZeroClipBoard使用以下JS代码:
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');
},
oncomplete之上是我的一个函数,它在某些操作上被调用.我从它得到的项目是html元素.现在在上面的代码中:
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 :
var text= $(item).html();//Not working when I hover the clip
//var text= 'Hello';// This is working when I hover the clip
如果我注释第一行而取消注释第二行,则该剪辑正在工作,并且文本已复制到剪贴板.但是我必须在复制文本时使用该html元素的值.那么我应该怎么做呢?此时,我正在获得控制的价值
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();//
var text= $(item).html();//
但是当调用悬停函数时,它会丢失.我以为它将通过Closure保存.我想念什么吗?我无法在此行获得text的值:
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);
当我在clip.addEventListener('mouseOver',function(client){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); })
推荐答案
该值不会保留在函数调用中,您需要使用$.proxy
:
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));
这篇关于关闭在ZeroClipboard中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!