复制文本到剪切板可使用navigator.clipboard.writeText()方法,如需兼容ie等老版本浏览器可使用execCommand()方法。文章来源地址https://www.toymoban.com/news/detail-643851.html
function textCopy(t) {
// 如果当前浏览器版本不兼容navigator.clipboard
if (!navigator.clipboard) {
var ele = document.createElement("input");
ele.value = t;
document.body.appendChild(ele);
ele.select();
document.execCommand("copy");
document.body.removeChild(ele);
if (document.execCommand("copy")) {
console.log("复制成功!");
} else {
console.log("复制失败!");
}
} else {
navigator.clipboard.writeText(t).then(function () {
console.log("复制成功!");
}).catch(function () {
console.log("复制失败!");
})
}
}
文章来源:https://www.toymoban.com/news/detail-643851.html
到了这里,关于js实现文本复制到剪切板的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!