vue 触发点击事件,实现代码拷贝

<template>
    <div>
        <p class="grid" @click="copy(1)">222</p>
    </div>
</template>

<script>
export default {
    data() {
        return {

        }
    },
    methods: {
        copy(item){
            const input = document.createElement('input')
            document.body.appendChild(input)
            input.setAttribute('value',item)
            input.select()
            if (document.execCommand('copy')) {
                document.execCommand('copy')
                this.open2()
            }
            document.body.removeChild(input)
        },
        open2() {
            this.$message({
                message: '复制成功',
                type: 'success'
            });
        }
    },
}

</script>

<style>
.grid{
    width: 20px;
    height: 20px;
    background: #0f0;
}
</style>

猜你喜欢

转载自blog.csdn.net/Amo__/article/details/89962743
今日推荐