如何用GWT复制到剪贴板?
通过 Google 搜索找不到任何相关信息。
有谁知道如何通过 GWT Java 代码将一些文本复制到剪贴板? 我想避免原始的 javascript 注入解决方案。
任何帮助或指示表示赞赏。
Couldn't find anything on this with a Google Search.
Does anyone know how to copy some text to the clipboard through GWT Java code?
I'd like to avoid the raw javascript injection solution.
Any help or pointers appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需包装提供的答案https://stackoverflow.com/a/30810322/106261。
因此,您将任何文本传递给 javascript 本机函数/方法,js 函数会创建一个新元素并复制到剪贴板,并在复制后删除该元素。
新浏览器不需要任何库。
所以 :
Just wrap the provided answer https://stackoverflow.com/a/30810322/106261.
So, you pass in any text to a javascript native function/method, the js function creates a new element and copies to clipboard, and removes the element after copying.
No need for any libs with new browsers.
so :
我已将 ZeroClipboard 与 GWT 一起使用(按照 Alexander 的建议),但这并不简单。
请参阅http://blog.dandoy.org/2011/09/使用-zeroclipboard-with-gwt.html
I have used ZeroClipboard with GWT (as suggested by Alexander) but it was not straightforward.
See http://blog.dandoy.org/2011/09/using-zeroclipboard-with-gwt.html
以下代码在 Chrome 中对我来说效果很好:
The following code worked fine for me in chrome:
GWT 本身并不支持
$doc.execCommand('copy');
命令,但它非常简单。首先将焦点设置在项目上,选择文本,然后复制它。
GWT doesn't natively support the
$doc.execCommand('copy');
command, but it's super easy.First set the focus on the item, select the text, then copy it.
目前似乎没有任何 GWT 库提供此功能。无论如何,不可能在所有浏览器中都支持这一点,因为需要 Flash。 ZeroClipboard 是一个相当不错的库,它不仅仅包含了功能。
For the moment it doesn't seem like there are any GWT libraries that provide this functionality. In any case, it's impossible to support this in all browsers as Flash is needed. A rather nice library than wraps the functionality is ZeroClipboard.
这里是一个没有原生 JS 的解决方案,而是 gwt elemental ,仍然受到 @SushmithaShenoy 的启发,将其留在这里以供将来参考。
前提条件:
现在是“真实”代码,可能放置在点击处理程序中:
Here a solution without native JS, but gwt elemental instead, still inspired by @SushmithaShenoy, leaving this here for future reference.
precondition:
now the 'real' code, maybe placed in a clickhandler: