<template>
<div>
<button @click="cpoy">点击复制文字内容</button>
</div>
</template>
<script>
function exeCommandCopyText(text) {
try {
const t = document.createElement('textarea')
t.nodeValue = text
t.value = text
document.body.appendChild(t)
t.select()
document.execCommand('copy');
document.body.removeChild(t)
return true
} catch (e) {
console.log(e)
return false
}
}
export default {
methods: {
cpoy() {
let content = `123
456
789` // 包含了换行符
let flag = exeCommandCopyText(content)
console.log(flag ? "复制成功" : "复制失败")
}
}
}
</script>
vue实现复制内容
文章介绍了如何使用JavaScript在前端实现点击按钮后复制特定的文字内容,包括创建`textarea`元素、设置值、选择文本并执行复制命令的过程。
摘要由CSDN通过智能技术生成