【问题标题】:Use Zeroclipboard.js with Meteor.js将 Zeroclipboard.js 与 Meteor.js 一起使用
【发布时间】:2015-07-24 05:11:01
【问题描述】:

按照此处的示例尝试让 Zeroclipboard.js 在 Meteor 应用程序中工作:http://www.thewebflash.com/2014/10/copy-to-clipboard-cross-browser-using.html

我在客户端文件夹中有最新的 ZeroClipboard.js 和 ZeroClipboard.swf。

在我的模板中,我有:

<template name="listPage">
<!-- some other markups -->
<input type="text" id="input1" />
<button type="button" id="btn-copy1" class="btn-copy">Copy to Clipboard</button>
<span class="span-message"></span>
</template>

与事件配对:

Template.listPage.events({
'click .btn-copy': function(e) {
    e.preventDefault();
    var client = new ZeroClipboard($('.btn-copy'));

    client.on('ready', function(event) {
        client.on('copy', function(event) {
            // `this` === `client`
            // `event.target` === the element that was clicked

            // Get the text content of <input> or <textarea> that comes immediately before the clicked button
            var $prevEle = $(event.target).prev();
            var text = $prevEle.is('textarea') ? $prevEle.val().replace(/\n/g, '\r\n') : $prevEle.val();

            // If value of `text` is not empty, set the data to clipboard
            if (text) {
                event.clipboardData.setData('text/plain', text);
            }
        });

        client.on('aftercopy', function(event) {
            // Check if copied text is not empty
            if (event.data["text/plain"]) {
                // Call something on successful copying
                $(event.target).next().stop().css('opacity', 1).text('Copied!').fadeIn(30).fadeOut(1000);
            }
        });
    });

    client.on('error', function(event) {
        ZeroClipboard.destroy();
    });
  }
});

我也试过https://github.com/zeroclipboard/zeroclipboard上的简单例子

不知道我做错了什么。非常感谢您的帮助。

【问题讨论】:

  • 由于浏览器和 Flash 安全限制,这种剪贴板注入只有在用户点击不可见的 Flash 影片时才会发生。来自 JavaScript 的模拟点击事件是不够的,因为这会导致剪贴板中毒。
  • github上的msg,你搞定了吗,那里有补丁
  • 所以我可能没有点击不可见的 Flash 电影,因此没有复制任何内容。

标签: javascript jquery meteor


【解决方案1】:

可能你把 zeroclipboard.swf 放在了错误的地方。

在浏览器“复制按钮”中检查。如果new ZeroClipboard($('.btn-copy')) 工作正常 - 你会看到类似的东西。

<object id="global-zeroclipboard-flash-bridge" 
name="global-zeroclipboard-flash-bridge" 
type="application/x-shockwave-flash" 
data="http://localhost:3000/client/lib/ZeroClipboard.swf?noCache=1432275841705" 
height="100%" width="100%">.........</object>

注意 ZeroClipboard.swf 的路径。在我的情况下,它是 /client/lib/ZeroClipboard.swf - 也就是说,我已将 ZeroClipboard.min.js 放入 /client/lib - 它会尝试在同一路径上搜索 swf。

这意味着您必须将 .swf 放入流星项目的 /public 文件夹中,并在那里创建与该路径匹配的子文件夹。我相信在创建新的 ZeroClipboard 实例时可以选择明确设置此路径 - 但 .swf 必须驻留在 /public 文件夹中。虽然它在 /client 文件夹中 - 它不可用。

另外,您确定它可以与类选择器 (new ZeroClipboard($('.btn-copy'))) 一起使用吗? id 选择器(#)返回单个项目,类选择器返回一个数组..

【讨论】:

  • 使用 ZeroClipboard.config({swfPath: '/path/to/ZeroClipboard.swf'}) 明确设置路径。这样做的原因:默认路径是:ZeroClipboard.swf 在 ZeroClipboard.js 所在的同一文件夹中。这意味着,它将尝试在开发中使用不同的路径(将是 /client/.. - 您在开发中放置 .js 文件的位置,在生产中放置 /)
  • 还有一点需要注意 - 构造函数不接受 jquery 元素 ($("#something")),而是接受 DOM 元素 (document.getElementById("something"))
猜你喜欢
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多