这是我在做一个一键复制文本功能的方法,在这里分享一下。
HTML文件:
<div class="email" id="email">12345679@qq.com</div>
<button class="button" (click)="copy()">Copy</button>
<textarea id="input">test</textarea>
TS文件
copy() {
const text = document.getElementById('email').innerText;
const input = document.getElementById('input') as HTMLTextAreaElement;
input.value = text; // 修改文本框的内容
input.select(); // 选中文本
document.execCommand('copy'); // 执行浏览器复制命令
}
关键是const input =document.getElementById('input') as HTMLTextAreaElement; 这一段中的as HTMLTextAreaElement,
使用之后编译的时候就不会报错了。