Vue+elementui+clipboard实现点击按钮复制文本域内容

  • 结果如下:
    在这里插入图片描述
    在这里插入图片描述

  • 示例代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.13.0/theme-chalk/index.css">
    <style>
        .el-row {
            margin-bottom: 20px;
        }

        .el-row:last-child {
            margin-bottom: 0;
        }
        #copyArea {
            position: absolute;
            width: 50%;

        }
        #copyText {
            position: relative;
            display: inline-block;
        }
        #copyBtn {
            /*display: inline-block;*/
            position: absolute;
            bottom: 5px;
            right: 1px;
        }
        /*html { overflow-x:hidden; }*/
        body {
            overflow-x:hidden;
            overflow-y:hidden;}

    </style>

</head>
<body >
<div id="app">
    <el-row :gutter="20">
        <el-col :offset="6" >
            <div id="copyArea">
                <el-input
                        id="copyText"
                        type="textarea"
                        :rows="30"
                        placeholder="请输入要复制的内容"
                        v-model="textarea">
                </el-input>
                <el-button round size="small" id="copyBtn" @click="clickCopyBtn(textarea, $event)"
                           data-clipboard-target="#copyText">复制
                </el-button>
            </div>
        </el-col>

    </el-row>




</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.bootcss.com/element-ui/2.13.0/index.js"></script>
<script src="https://cdn.bootcss.com/clipboard.js/2.0.6/clipboard.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                textarea: ' '
            }
        },
        methods: {
            clickCopyBtn: function (text, event) {

                const btnId = '#' + event.currentTarget.id;
                const clipboard = new ClipboardJS(btnId, {
                    text: () => text
                });
                // const clipboard = new ClipboardJS(event.target);

                clipboard.on("success", e => {

                    this.$message({
                        type: "success",
                        message: "复制成功",
                        duration: 300,
                        showClose: true,
                    }); //这里你如果引入了elementui的提示就可以用,没有就注释即可

                    // 释放内存

                    clipboard.destroy();

                });
                clipboard.on("error", e => {

                    // 不支持复制

                    this.$message({
                        type: "error",
                        message: "复制失败",
                        duration: 300,
                        showClose: true,
                    });
                    clipboard.destroy();

                });
            }
        }
    })
</script>
</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值