【发布时间】:2019-06-08 07:02:08
【问题描述】:
我想将此 for 循环的内容复制到剪贴板:
<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>
我跟着this的回答想出了这个方法:
handleCopy() {
this.$refs.text.select();
document.execCommand('copy');
}
但这会导致:
Uncaught TypeError: this.$refs.text.select is not a function
所以我不知道如何在不使用第三方 javascript 插件的情况下解决这个问题?
P.S.我尝试了一些 JS 特定的建议答案,例如 this,但得到错误:
Uncaught TypeError: Failed to execute 'selectNode' on 'Range': parameter 1 is not of type 'Node'.
【问题讨论】:
-
您可以简单地将数据镜像到隐藏的
<textarea>并使用它。 -
您需要将
innerHTML放入textarea中,然后选择并复制它。 -
@ChrisG 我该怎么做?
-
How do I copy to the clipboard in JavaScript? 的可能重复项更具体地说 - this answer
-
对我来说很好:codesandbox.io/s/484yxqpmm4
标签: javascript vue.js