如何在 jQuery 中实际使用 ZeroClipboard?
我就是无法得到这个东西。 ZeroClipboard 应该如何工作?为什么需要将 flash 元素移动到复制的文本上?
我读过这个东西:http://code.google.com/p/zeroclipboard /wiki/Instructions
有人可以给我提供一个简短的代码片段,当用户单击链接时,它可以将变量中的文本复制到用户剪贴板。这可能吗?我不在乎,如果它不能在所有浏览器上运行(例如 IE6)。
我正在使用 jQuery。
I just can't get this thing. How is ZeroClipboard supposed to work? Why does it need to move the flash-element over the copied text?
I've read this thing: http://code.google.com/p/zeroclipboard/wiki/Instructions
Can someone provide me a short snippet, which makes it possible to copy a text in variable to users clipboard, when user clicks a link. Is this even possible? I don't care, if it doesn't work on all browsers (IE6 for example).
I'm using jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您链接到的页面上给出的“最小示例”代码 (http:// code.google.com/p/zeroclipboard/wiki/Instructions#Minimal_Example)似乎就是您想要的。我已将其复制到此处并对其进行了更改以演示将文本放入变量中,然后将该文本复制到剪贴板,因为这就是您感兴趣的内容。请注意,在现实生活中,您可能想要做的是在某个函数中调用
clip.setText()
部分,因为在页面首次加载时您可能不知道要复制什么文本。Flash 元素不需要“位于复制的文本之上”;它需要“粘合”到您希望用户操作的任何 DOM 元素——很可能是一个要单击的按钮。原因是Javascript无法访问剪贴板,因此您需要使用Flash。但 Flash 只能在用户的计算机上运行以响应用户的点击,因此您可以通过将 Flash 作为 HTML 元素上的不可见覆盖层来“诱骗”用户点击 Flash。
我要指出的是,虽然复制到用户剪贴板的特定示例可能是良性的,但这种方法让我感到困扰,因为不难想象隐藏的 Flash 元素会做更多恶意的事情。
The "minimal example" code given on the page you link to (http://code.google.com/p/zeroclipboard/wiki/Instructions#Minimal_Example) appears to be what you want. I've copied it here and altered it to demonstrate putting text into a variable and then copying that text to the clipboard, since that's what you're interested in. Note that, in real life, what you'd presumably want to do is call the
clip.setText()
part within some function, since you might not know, at the point when the page is first loaded, what text you want to copy.The flash element doesn't need to be "over the copied text"; it needs to be "glued" to whatever DOM element you want your user to manipulate -- most likely a button to click. The reason is that Javascript doesn't have access to the clipboard, so you need to use Flash instead. But Flash can only operate on the user's machine in response to a user's click -- so you "trick" the user into clicking on the Flash by making it an invisible overlay over an HTML element.
I'll note that while the particular example of copying to the user's clipboard is probably benign, this approach troubles me, as it wouldn't be hard to imagine the hidden flash element doing more malicious thing.
一个稍微复杂一点的 jquery 示例。复制文本时
A slightly more complex jquery example. Copy the text when
此代码仅在我的设置中与 chrome 一起工作,当我的主体区域中有一个标签时,例如
当文件包含被删除时,按钮不起作用......真的很奇怪
This code only works in my setup together with chrome when I have a tag in my body-area like
when the file-include is removed the button is not working.... really strange
当前版本的 ZeroClipboard 实际上包含一个错误,该错误会导致使用 JacobM 建议的脚本出现 JS 错误 - 在以下情况下:
为了避免导致错误,应使用 mouseover 事件侦听器来设置文本:
The current version of ZeroClipboard actually contains a bug that would cause an JS error using the script suggested by JacobM - in the following scenario:
To avoid causing errors, the mouseover event listener should instead be used to set the text: