我想将这个 for-loop 的内容复制到剪贴板:
<div ref="text" class="links">
<div class="row" v-for="(name, index) in resultNames" :key="index" >
<p>{{makeUrl(name)}} </p>
</div>
</div>
<button @click="handleCopy">Copy to Clipboard</button>
我按照 这个 答案想出了这个方法:
handleCopy() {
this.$refs.text.select();
document.execCommand('copy');
}
但这导致:
Uncaught TypeError: this.$refs.text.select is not a function
所以我不知道如何在不使用第三方 javascript 插件的情况下解决这个问题?
PS 我尝试了一些 JS 特定的建议答案,比如 这样,但出现错误:
Uncaught TypeError: Failed to execute 'selectNode' on 'Range': parameter 1 is not of type 'Node'.
原文由 qliq 发布,翻译遵循 CC BY-SA 4.0 许可协议
基于 这个 答案,这里有一个选择
HTMLElement
的文本的函数:剩下要做的是 a) 传递元素 b) 调用复制命令: