【发布时间】:2019-12-12 13:20:08
【问题描述】:
我的网站url 是http://localhost:4200/reader-main。我希望在单击anchor 链接时复制它。复制的链接可以粘贴到任何地方。
代码:
HTML:
<div class="row permalink">
<a id="copyPermalink" class="copyLink" href="" (click)="copyToClipboard($event)" aria-label="Copy Permalink">Copy Permalink</a>
</div>
Javascript:
copyToClipboard(event: any): void {
const copyUrl = document.querySelector('copyLink');
window.getSelection();
try {
const successful = document.execCommand('copy');
const message = successful ? 'Successful' : 'Unsuccessful';
console.log(copyUrl);
alert(message);
} catch (err) {
console.log('Not able to copy', err);
}
return false;
}
但在上面的代码中,console.log(copyurl) 返回 null。并且 URL 没有被复制。如何解决?
【问题讨论】:
-
这个答案对你有帮助吗? stackoverflow.com/questions/1034621/…
标签: javascript