为了提升用户体验,或者放置用户复制文本的时候,出现多复制或者少复制或者格式不对的情况,一件复制剪切板功能就有用了
/**
* 复制内容到粘贴板
* contentText : 需要复制的内容
* msgTip : 复制完后的提示,不传则默认提示"复制成功"
*/
const copyToClip = (contentText:string, msgTip:string) => {
let inputEle = document.createElement("input");
inputEle.setAttribute("value", contentText);
document.body.appendChild(inputEle);
inputEle.select();
document.execCommand("copy");
document.body.removeChild(inputEle);
if (msgTip == null) {
alert("复制成功。");
} else{
alert(msgTip);
}
};