JavaScript将文本复制到剪贴板[重复]

时间:2022-11-09 10:44:27

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>

1 个解决方案

#1


22  

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及更高版本。当您运行它时,可能会出现一个对话框,询问您是否“希望此网站能够访问您的剪贴板”。如果是,请单击是。用户在框中输入的任何文本都将被复制到剪贴板。

#1


22  

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及更高版本。当您运行它时,可能会出现一个对话框,询问您是否“希望此网站能够访问您的剪贴板”。如果是,请单击是。用户在框中输入的任何文本都将被复制到剪贴板。