JavaScript 将文本复制到剪贴板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7218061/
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
JavaScript copy text to clipboard
提问by Tony
Possible Duplicate:
Copy selected text to the clipboard WITHOUT using flash - must be cross-browser
This one has kept me going for a long time. How would I copy text to the clipboard? Here is my code:
这个让我坚持了很长时间。如何将文本复制到剪贴板?这是我的代码:
<body>
<textarea name="text" rows="5" cols="20" wrap="hard" onblur="CopyToClipboard()">Enter text here and it will be copied to the clipboard!</textarea>
</body>
<script type="text/javascript">
function CopyToClipboard() {
//O_O Confused... what do I do...
}
</script>
采纳答案by fireshadow52
Here is one way you can do it...
这是您可以做到的一种方法......
<body>
<textarea rows="5" cols="20" wrap="hard" onblur="CopyToClipboard(this)"></textarea>
</body>
<script language="JavaScript">
function CopyToClipboard(text) {
Copied = text.createTextRange();
Copied.execCommand("Copy");
}
</script>
This only works with IE 4 and above. When you run it, a dialog may come up asking you whether or not "you want this website to have access to your clipboard". Click yes if it does. Whatever text the user entered into the box will be copied to the clipboard.
这仅适用于 IE 4 及更高版本。当您运行它时,可能会出现一个对话框,询问您是否“希望该网站能够访问您的剪贴板”。如果是,请单击是。用户在框中输入的任何文本都将复制到剪贴板。