有没有什么方法可以选择多行 asp:textbox 中的所有文本,并使用 C# 通过单击按钮将其复制到客户端剪贴板?
提前谢谢你。
最佳答案
您可以使用 document.execCommand("copy");
请注意,大多数新浏览器都支持此功能,据我所知,不支持 Safari:
<head runat="server">
<title></title>
<script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnCopy").click(function () {
var id = "#" + "<%= txtText.ClientID %>";
try {
$(id).select();
document.execCommand("copy");
}
catch (e) {
alert('Copy operation failed');
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtText" runat="server" Text="Some sample text to copy"></asp:TextBox>
<button id="btnCopy">Copy</button>
</form>
</body>
经过测试并适用于以下浏览器:
- IE 11 及更高版本
- 谷歌浏览器 51.0.2704.84
- 火狐 43.0.1
关于c# - ASP :textbox select all text by click button and copy to client clipboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37784995/