html
<p ref="p">這是文字</p> <button @click="copy">點擊復制上面內容</button>
js
data() { return { copyText: "" }; }, methods: { copy() { this.copyText = this.$refs.p.innerText; var input = document.createElement("input"); // 直接構建input input.value = this.copyText; // 設置內容 console.log(input.value); document.body.appendChild(input); // 添加臨時實例 input.select(); // 選擇實例內容 document.execCommand("Copy"); // 執行復制 document.body.removeChild(input); // 刪除臨時實例 }, }
也可以長按復制內容,長按事件,看上一篇。
