我正在尝试在网页上实现复制到剪贴板按钮。下面是我写的代码
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 and Safari。是否有任何更改/替代实现可以用来在我的网页上实现此目的。
不确定 Safari,但在 IE 上你可以这样做:
window.clipboardData.setData('Text', 'text you want to copy goes here');
我希望它有帮助。干杯
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)