简体   繁体   English

将其分配给隐藏div中的多个链接时出现zclip问题

[英]zclip issue when assigning it to multiple links in hidden divs

I m using ZClip (http://www.steamdev.com/zclip/) to enable a 'copy to clipboard' feature on a webpage on multiple links. 我正在使用ZClip(http://www.steamdev.com/zclip/)在网页上的多个链接上启用“复制到剪贴板”功能。 I m facing 2 issues in this scenario: 在这种情况下,我面临2个问题:

  1. In the page where ZClip is used, there are 4 divs listing out image files, video files, audio files and documents that a user had previously uploaded. 在使用ZClip的页面中,有4个div列出了用户以前上传的图像文件,视频文件,音频文件和文档。 Each of these divs is seen one at a time, so for example, a user would click on the audio tab to view all audio files, then if he clicked on the videos tab, the div that showed the audio files would be hidden and the one for videos would be shown and so on. 每个div一次可见一次,因此,例如,用户单击“音频”选项卡以查看所有音频文件,然后,如果他单击“视频”选项卡,则显示音频文件的div将被隐藏,并且其中一个会显示视频,依此类推。 Maybe because ZClip uses Flash, it is not able to 'load' its dependent swf in a HTML element whose parent element was hidden. 也许是因为ZClip使用Flash,所以它无法在其父元素被隐藏的HTML元素中“加载”其依赖的swf。 So I load the zclip instances on clicking each of the tabs. 因此,我在单击每个选项卡时加载zclip实例。 This is problem one. 这是问题之一。 I d like to load it once and not have to keep reloading everytime a tab was clicked. 我想加载一次,而不必每次单击选项卡时都保持重新加载。
  2. I have enabled a zclip afterCopy action on each of the links while initializing zclip on them. 我在每个链接上初始化zclip时都对每个链接启用了zclip afterCopy操作。 In this afterCopy function a alert box is displayed to inform the user that the text was copied to the clipboad. 在此afterCopy功能中,将显示一个警告框,通知用户该文本已复制到剪贴板。 Now since I m loading zclip instance every time a tab is clicked, the afterCopy action is getting applied more than once and hence the alert box pops up more than once. 现在,由于我每次单击选项卡时都加载zclip实例,因此afterCopy操作将被多次应用,因此警报框会弹出多次。 SO if I came to the videos tab twice and clicked a copy link button then I d see 2 alert boxes. 因此,如果我两次进入“视频”标签并单击“复制链接”按钮,那么我会看到2个警报框。 If I came on the video tab thrice then 3 alert boxes. 如果我在“视频”标签上出现三次,则出现3个警报框。

I ve tried using the $('a.copy').zclip('remove'); 我试过使用$('a.copy').zclip('remove'); before initializing zclip on links. 在初始化链接上的zclip之前。 This feature is shown on the zclip website but it has not resulted in getting rid of the extra alert boxes. 此功能显示在zclip网站上,但并未消除多余的警告框。 It only removes the swf associated with the links and not the events bound to the links. 它仅删除与链接关联的swf,而不删除绑定到链接的事件。 Basically I d like some guidance in how I can do either of the following: 基本上,我希望获得有关如何执行以下任一操作的指导:

  1. 'unbind' zclip from existing items it was applied on before applying it on selecting a tab. 在选择选项卡之前将其从已应用的现有项目中“解除绑定”。
  2. or some way that zclip is applied on multiple links only once regardless of the fact that the links its applied on are inside hidden divs 或以某种方式将zclip仅应用到多个链接一次,无论其应用的链接是否在隐藏的div内
  3. or better still, use only one single zclip instance to use on multiple 'copy' links. 甚至更好的是,仅使用一个zclip实例在多个“复制”链接上使用。 So if each link that was clicked to copy something to the clipboard, it d use the same zclip instance. 因此,如果每个被单击以将内容复制到剪贴板的链接都将使用相同的zclip实例。

Saganbyte, Saganbyte,

A couple of approaches come to mind. 我想到了几种方法。

First, let's assume the HTML of each of your four image/video/audio/documents divs is something like this: 首先,让我们假设四个图像/视频/音频/文档div的每个HTML都是这样的:

<div class="content" ...>
    ...
    <input class="copyMe" /><!-- the element whose value is to be copied -->
</div>

Both approaches rely on a single "copy" button, which is visble when zClip is initialized: 两种方法都依赖于单个“复制”按钮,在初始化zClip时可见:

<a id="copy">Copy</a>

Your HTML is undoubtedly different but it should be fairly simple to adapt the ideas below to suit. 您的HTML毫无疑问是不同的,但是要适应以下想法,应该相当简单。

Move "copy" button to the active div 将“复制”按钮移至活动div

This solution relies on : 该解决方案依赖于:

  • placing the "copy" button anywhere in the DOM, providing it is visible on page load 将“复制”按钮放置在DOM中的任何位置,只要它在页面加载时可见
  • making the "copy" button work relative to its current position 使“复制”按钮相对于其当前位置起作用
  • providing in each content div an empty element (eg. a span or div) with class="copyWrapper" , into which the "copy" button can be moved 在每个内容div中提供一个带有class="copyWrapper"的空元素(例如span或div),可以将“ copy”按钮移动到其中
  • moving the "copy" button into the active panel's .copyWrapper element each time a tab is clicked. 每次单击选项卡时,将“复制”按钮移动到活动面板的.copyWrapper元素中。

Initialize zClip as follows: 如下初始化zClip:

var $copyButton = $('a#copy').zclip({
    path: 'js/ZeroClipboard.swf',
    copy: function() {
        return $(this).closest('div.content').find('.copyMe').val(); //$(this) is assumed correct
    }
});

And initialize the tabs (assuming jQuery UI "tabs") as follows : 并按如下所示初始化标签(假设jQuery UI为“标签”):

$(".selector").tabs({
    ...
    show: function(event, ui) {
        $(ui.panel).find('.copyWrapper').append($copyButton);
    }
});

Static "copy" button 静态“复制”按钮

This solution relies on : 该解决方案依赖于:

  • changing the page design to place the "copy" button outside the content divs 更改页面设计以将“复制”按钮放置在内容div之外
  • making the "copy" button work on whichever content div is currently visible. 使“复制”按钮在当前可见的内容div上起作用。

Initialize zClip as follows: 如下初始化zClip:

$('a#copy').zclip({
    path: 'js/ZeroClipboard.swf',
    copy: function(){
        return $('.content:visible').find('.copyMe').val();
    }
});

Dynamic one-time zClip initialization 动态一次性zClip初始化

This solution relies on : 该解决方案依赖于:

  • initializing each div's "copy" buttons (pleural) when they are first shown 首次显示每个div的“复制”按钮(胸膜)时进行初始化
  • raising a boolean flag on initialization to inhibit further attempts to re-initialize when tabs are re-visited. 在初始化时引发布尔值标志,以禁止在重新访问选项卡时进一步尝试重新初始化。

javascript: JavaScript的:

$(".selector").tabs({
    ...
    show: function(event, ui) {
        var $panel = $(ui.panel);
        if(!$panel.data('zClip_initialized')) { //If zClip not initialized in theis panel, then initialize it.
            $('a.copy').zclip({
                path: 'js/ZeroClipboard.swf',
                copy: function() {
                    return $(this).closest('tr').find('.....').val();//Lots of guesswork here. You should have written this already.
                }
            });
            $panel.data('zClip_initialized', true);//Raise a boolean flag to indicate that zClip is already initialized
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM