javascript 复制到剪贴板为 IE 几乎工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15958441/
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 to Clipboard for IE almost working?
提问by user1839308
Trying get this script to copy to the clipboard and not back into the page. When you click the link it should copy right to the clip board. At least that is my intention. Here are some background facts behinds it:
尝试将此脚本复制到剪贴板而不是返回页面。当您单击该链接时,它应该会直接复制到剪贴板。至少这是我的意图。以下是其背后的一些背景事实:
- This is for a company intranet site that uses IE exclusivly so it does not need to be compatible with any other browsers
- The data inside is/will be a return from a db query
I realize this is old technology but it needs to be this way for now.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"><!-- // input field descriptions var desc = new Array(); desc['a1'] = 'First name'; desc['a2'] = 'Last name'; desc['a3'] = 'Address'; desc['a4'] = 'Zip'; desc['a5'] = 'City'; desc['a6'] = 'Country'; function CopyFields(){ var copytext = ''; for(var i = 0; i < arguments.length; i++){ copytext += desc[arguments[i]] + ': ' + document.getElementById(arguments[i]).innerText + '\n';} var tempstore = document.getElementById(arguments[0]).innerText; document.getElementById(arguments[0]).innerText = copytext; document.getElementById(arguments[0]).focus(); document.getElementById(arguments[0]).select(); document.execCommand('Copy'); document.getElementById(arguments[0]).innerText = tempstore; } </script> </head> <body> <table> <tr> <td id="a1" name="t1">a</td> <td id="a2" name="t2">b</td> <td id="a3" name="t3">c</td> <td id="a4" name="t4">d</td> <td id="a5" name="t5">e</td> <td id="a6" name="t6">f</td> </tr> </table><br> <a href="#" onClick="CopyFields('a1', 'a2', 'a3', 'a4', 'a5', 'a6');">Copy values of text fields to clipboard</a> </body> </html>
- 这是一个公司内网网站,只使用IE浏览器,所以不需要兼容任何其他浏览器
- 里面的数据是/将是 db 查询的返回
我意识到这是旧技术,但现在需要这样。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"><!-- // input field descriptions var desc = new Array(); desc['a1'] = 'First name'; desc['a2'] = 'Last name'; desc['a3'] = 'Address'; desc['a4'] = 'Zip'; desc['a5'] = 'City'; desc['a6'] = 'Country'; function CopyFields(){ var copytext = ''; for(var i = 0; i < arguments.length; i++){ copytext += desc[arguments[i]] + ': ' + document.getElementById(arguments[i]).innerText + '\n';} var tempstore = document.getElementById(arguments[0]).innerText; document.getElementById(arguments[0]).innerText = copytext; document.getElementById(arguments[0]).focus(); document.getElementById(arguments[0]).select(); document.execCommand('Copy'); document.getElementById(arguments[0]).innerText = tempstore; } </script> </head> <body> <table> <tr> <td id="a1" name="t1">a</td> <td id="a2" name="t2">b</td> <td id="a3" name="t3">c</td> <td id="a4" name="t4">d</td> <td id="a5" name="t5">e</td> <td id="a6" name="t6">f</td> </tr> </table><br> <a href="#" onClick="CopyFields('a1', 'a2', 'a3', 'a4', 'a5', 'a6');">Copy values of text fields to clipboard</a> </body> </html>
回答by Dr.Molle
there is no select()
-method for td
-elements.
-elements没有select()
-method td
。
You may directly access the clipboard without using the Copy-command:
您可以直接访问剪贴板而不使用 Copy 命令:
window.clipboardData.setData('Text', copytext);
See: http://msdn.microsoft.com/en-us/library/ie/ms536744%28v=vs.85%29.aspx
请参阅:http: //msdn.microsoft.com/en-us/library/ie/ms536744%28v=vs.85%29.aspx