Javascript 使用零剪贴板复制文本区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12340331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy Textarea with Zero Clipboard
提问by Chris Burton
Is there a way to copy the textarea to the clipboard without a button?
有没有办法在没有按钮的情况下将 textarea 复制到剪贴板?
What I'm trying to do is have a Short URL display on my site so when you click the link (or text), it copies to the clipboard and fades out with new text fading in that says "Copied!". The links/text will automatically be generated with bit.ly with my custom domain.
我想要做的是在我的网站上显示一个短 URL,因此当您单击链接(或文本)时,它会复制到剪贴板并随着新文本淡出而淡出,上面写着“已复制!”。链接/文本将使用我的自定义域通过 bit.ly 自动生成。
Example:
例子:
Short URL: shrt.lnk/abc123 (Regular) Short URL: Copied! (On Click)
短网址:shrt.lnk/abc123(常规)短网址:已复制!(点击)
Anyone have an idea how to do this?
任何人都知道如何做到这一点?
回答by Triforcey
You could try clip-j instead. Hereit is. This is a pure JavaScript solution that takes advantage of document.execCommand('copy');
witch is supported by all major browsers, with other commands to work around the limitation of having to be able to see a text input, do you doing see anything! Plus this doesn't have to work through a button, clicking a link will work just fine!
你可以试试clip-j。在这里。这是一个纯粹的 JavaScript 解决方案,它利用document.execCommand('copy');
了所有主要浏览器都支持的女巫,还有其他命令可以解决必须能够看到文本输入的限制,你有没有看到任何东西!另外,这不必通过按钮工作,点击链接就可以了!
回答by sajawikio
It's impossible to do it without the button because Flash 10+ requires a click from the actual button. However, if you use the actual button with text in it, or superimpose button on the text as done here: http://jsfiddle.net/C9KjHthen it is a possible workaround in your case.
没有按钮是不可能做到的,因为 Flash 10+ 需要点击实际按钮。但是,如果您使用带有文本的实际按钮,或者像此处所做的那样将按钮叠加在文本上:http: //jsfiddle.net/C9KjH那么在您的情况下这是一种可能的解决方法。