简体   繁体   English

不论浏览器如何复制textarea文本

[英]copying textarea text regardless of browser

I"m trying to do something that I originally thought would be quite simple, but it's turning out not to be. Mainly I'm trying to have so I can have a textarea on a page, with a button that would allow the user to copy everything within the textarea to paste wherever they wish. However after slogging through a few attempts and a lot of applications of google-fu, I've yet to have an example that works, much less is cross browser compatible. 我正在尝试做一些我原本认为很简单的事情,但事实并非如此。主要是我想这样做,所以我可以在页面上有一个文本区域,并带有一个允许用户执行操作的按钮复制文本区域中的所有内容以粘贴到所需的位置,但是经过几次尝试和大量google-fu应用程序的困扰之后,我还没有一个可行的示例,更不用说跨浏览器兼容了。

So I guess my question is two fold; 所以我想我的问题有两个方面。 is this possible, and if so, is this able to be done so that the browser used isn't a factor? 这是否可能,如果可以,是否可以做到这一点,以使所使用的浏览器不是一个因素?

Thanks for your help and responses to this question. 感谢您的帮助和对此问题的答复。

This is possible, but isn't consistent across browsers. 这是可能的,但在浏览器之间并不一致。 You can use the jQuery plugin Clipboard as a substitute. 您可以使用jQuery插件剪贴板替代。

This plugin uses a Flash object internally to copy to the clipboard, which should be consistent across browsers. 此插件内部使用Flash对象复制到剪贴板,这在各个浏览器之间应保持一致。

With the plugin included, the following code should work: 使用附带的插件,以下代码应该可以工作:

var text = $("textarea").text();
$.clipboard(text);

That will copy everything in the text area to the clipboard. 这会将文本区域中的所有内容复制到剪贴板。

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

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