首页 > 代码库 > 选择文本框的文字并复制

选择文本框的文字并复制

var dLabel = document.getElementById("dLabel");

dLabel.addEventListener(‘click‘, function (event) {
var pageUrl = window.location.href;
var copyTextarea = document.getElementById("pageUrlInput");
copyTextarea.value = http://www.mamicode.com/pageUrl;

$("#copyInput").css({ "display": "" });

copyTextarea.select();
try {
var successful = document.execCommand(‘copy‘);
var msg = successful ? ‘successful‘ : ‘unsuccessful‘;
console.log(‘Copying text command was ‘ + msg);
} catch (err) {
console.log(‘Oops, unable to copy‘);
}
});

选择文本框的文字并复制