背景:vue实现点击按钮将指定文字复制功能
<template> <div> <input type="text" v-model="textToCopy"> <button @click="copyText">复制文本</button> </div> </template> <script> export default { data() { return { textToCopy: '要复制的文本内容' }; }, methods: { copyText() { const textarea = document.createElement('textarea'); textarea.value = this.textToCopy; document.body.appendChild(textarea); textarea.select(); document.execCommand('copy'); document.body.removeChild(textarea); alert('已复制到剪贴板'); } } } </script>