点击按钮复制文本框内容

第一种方法

function HighlightAll() {
var target = document.getElementById("txtLecturerAndStuName");
target.select();// 选择文本
document.execCommand("Copy"); // 执行浏览器复制命令
alert("已复制好,可贴粘。");
}

<asp:TextBox class="txtcontent" ID="txtLecturerAndStuName" MaxLength="200" runat="server"></asp:TextBox>
<input type="button" value="复制" onclick="HighlightAll()" />

第二种方法

function copyText(text, callback){ // text: 要复制的内容, callback: 回调
var tag = document.createElement('input');
tag.setAttribute('id', 'cp_hgz_input');
tag.value = text;
document.getElementsByTagName('body')[0].appendChild(tag);
document.getElementById('cp_hgz_input').select();
document.execCommand('copy');
document.getElementById('cp_hgz_input').remove();
if(callback) {callback(text)}
}
function HighlightAll()
{
var target = document.getElementById("<%=txtLecturerAndStuName.ClientID %>").value;
copyText(target, function () { alert('已复制好,可贴粘。') })
}

posted @ 2019-12-19 14:51  肖云鹏  阅读(770)  评论(0编辑  收藏  举报