vue实现复制内容

文章介绍了如何使用JavaScript在前端实现点击按钮后复制特定的文字内容,包括创建`textarea`元素、设置值、选择文本并执行复制命令的过程。
摘要由CSDN通过智能技术生成
<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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值