焦点自动复制到剪贴板
我有一个包含信息的文本区域。它有一个 onfocus 属性,可以自动选择其中的所有文本(onfocus =“this.select();)。我希望当我专注于文本区域时,所有选定的文本都会自动复制到我的剪贴板,这样我就可以轻松粘贴无需右键单击即可复制或按 CTRL + C 即可将其复制到任何地方。这是如何完成的?
I have a textarea that includes information. It has an onfocus attribute which selects all text inside it automatically (onfocus="this.select();). I want that when I focus on the textarea, all the selected text get's automatically copied to my clipboard, so I can easily paste it anywhere without having to right click to copy or pressing CTRL + C. How would this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,出于安全原因,浏览器的 JavaScript 引擎无法访问系统的任何剪贴板功能,因此,如果您要求一个可移植的、纯 JavaScript 的答案,那么恐怕您做不到。
(如果必须的话,您可以在网站上搜索“javascript”和“剪贴板”,我认为有一些使用 Flash 的方法。但最好根本不要这样做。)
In general, a browser's JavaScript engine does not have access to any of the system's clipboard features for security reasons, so if you are asking for a portable, pure-JavaScript answer, then I'm afraid you can't do it.
(If you must, you can search around the site for "javascript" and "clipboard", I think there are some approaches using Flash. But best not to do it at all.)
出于安全原因,JavaScript 无法访问剪贴板。
如果你真的想这样做,你必须使用flash(flash可以写入剪贴板,但不能读取)。
然而,您需要单击Flash内部才能写入剪贴板(但可以模拟)。
您可以在 Pastebin (复制到剪贴板)中看到它的完成情况。
如果您确实想这样做,我建议您使用Zero Clipboard。
请注意,您的实际方法是由 Google URL Shortener 使用的(尝试缩短网址)。
告诉我们您的决定。
For security reasons, JavaScript can't access the clipboard.
If you really want to do it, you must use flash (flash can write into the clipboard, but not read).
Yet, you need to click inside flash to write into clipboard (but it can be simulated).
You can see it being done in Pastebin (Copy to Clipboard).
If you really want to do it, I recommend you to use Zero Clipboard.
Notice that your actual approach is used by Google URL Shortener (try shorten a url).
Tell us your decision.
HTML5 API 系列中的 API 之一即将解决您的问题。它被称为“剪贴板 API 和事件”,它将通过 Javascript 为 Web 应用程序启用常见的剪贴板活动(剪切、复制、粘贴)。目前(截至 2011 年 7 月)它是一个工作草案。您可以在此处找到规范:
http://www.w3.org/TR/clipboard-apis /
至于实现,我还没有找到。给点时间...
One of the APIs in the HTML5 family of APIs is about to solve your problem. It's called "Clipboard API and Events," and it will enable common clipboard activities (cut, copy, paste) for web apps via Javascript. Its currently (as of July 2011) a Working Draft. You can find the spec here:
http://www.w3.org/TR/clipboard-apis/
As for implementations, well, I couldn't find one just yet. Give it time...
或转到 about:config 并在列表中搜索此项:
“clipboard.autocopy = false”将其更改为true! :D
or go to about:config and search this item on the list:
"clipboard.autocopy = false" change it true! :D