IE9,IE11和Safari的复制到剪贴板选项 [英] Copy to clipboard option for IE9, IE11 and Safari

查看:76
本文介绍了IE9,IE11和Safari的复制到剪贴板选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在网页上实现复制到剪贴板按钮。下面是我编写的代码

I am trying to implement a copy-to-clipboard button on a webpage. Below is the code I have written

function copyToClipboard(element) {
   var $temp = $("<input>");
   $("body").append($temp);
   $temp.val($(element).text()).select();
   document.execCommand("copy");
   $temp.remove();
}

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
   </script>

   <p id="p1">Text1</p>
   <p id="p2">Text2</p>
   <button onclick="copyToClipboard('#p1')">Copy Text1</button>
   <button onclick="copyToClipboard('#p2')">Copy Text2</button>
   <br/><br/>
   <input type="text" placeholder="Paste here for test" /> 

但是,这似乎在 IE上不起作用9、11 Safari 。我可以在网页上进行任何更改/替代实现。

However, this does not seem to work on IE 9, 11 and Safari. Is there any change/alternate implementation I can use to implement this on my webpage.

推荐答案

不确定Safari,但在IE上您可以这样做:

Not sure about Safari, but on IE you can do:

window.clipboardData.setData('Text', 'text you want to copy goes here');

我希望它会有所帮助。欢呼声

I hope it helps. Cheers

这篇关于IE9,IE11和Safari的复制到剪贴板选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆