Vue/html中点击复制到剪贴板

1.使用JQ实现复制功能

html

html 复制代码
   <div class="tran_one tran_yi">
        <div class="form-group">
            <textarea>文本</textarea>
        </div>
        <div class="form-group tran_group">
            <div class="tran_flex tran_left tran_copy tran_copy_one">
                <p>复制</p>
            </div>
        </div>
    </div>
    </div>
    <div class="tran_one tran_two">
        <div class="form-group">
            <textarea></textarea>
        </div>
        <div class="form-group tran_group">
            <div class="tran_flex tran_left tran_copy tran_copy_two">
                <p>复制</p>
            </div>
        </div>
    </div>

jq 封装后

javascript 复制代码
 function copyToClipboard(selector, alertText) {
        // 获取指定选择器下的复制板块
        var $copyBlock = $(selector);

        // 获取textarea元素
        var $textarea = $copyBlock.find('textarea');

        // 复制内容到剪贴板
        if ($textarea.length > 0) {
            $textarea.select();
            document.execCommand('copy');

            // 显示复制成功的提示信息
            var $alert = $('<div class="copyAlert">' + (alertText || '复制成功') + '</div>');
            $copyBlock.append($alert);
            setTimeout(function () {
                $alert.fadeOut(function () {
                    $(this).remove();
                });
            }, 3000);
        }
    }

    $('.tran_copy').click(function () {
        copyToClipboard($(this).closest('.tran_one'));
    });

    $('.tran_copy_two').click(function () {
        copyToClipboard($(this).closest('.tran_two'));
    });

2.使用Vue实现复制功能

html

html 复制代码
    <div class="tran_one tran_yi">
        <div class="form-group">
            <textarea>{{mainTransContent}}</textarea>
        </div>
        <div class="form-group tran_group">
            <div class="tran_flex tran_left tran_copy tran_copy_one"
                @click="handleTranslateModalCopy(mainTransContent)">
                <p>复制</p>
            </div>
        </div>
    </div>

创建了一个隐藏的textarea元素,并将需要复制的内容赋值给它,然后通过document.execCommand('copy')执行复制命令

javascript 复制代码
  copyToClipboard(text) {
        const textarea = document.createElement('textarea');
        textarea.style.position = 'fixed';
        textarea.style.top = 0;
        textarea.style.left = 0;
        textarea.style.width = '1px';
        textarea.style.height = '1px';
        textarea.style.padding = 0;
        textarea.style.border = 'none';
        textarea.style.outline = 'none';
        textarea.style.boxShadow = 'none';
        textarea.style.background = 'transparent';
        textarea.value = text;

        document.body.appendChild(textarea);
        textarea.select();
        try {
            const successful = document.execCommand('copy');
            if (successful) {
                this.$message({
                    message: '内容已成功复制到剪贴板!',
                    type: 'success',
                });
            } else {
                console.log('复制失败');
            }
        } catch (err) {
            console.log('复制失败:', err);
        }

        document.body.removeChild(textarea);
    },
相关推荐
变与不变8068 小时前
jQuery怎么使用
前端·javascript
神膘护体小月半8 小时前
flex 布局的 order 属性
前端·css
paopaokaka_luck8 小时前
流浪动物救助与收养系统(DeepSeek AI对话、 ECharts多维分析、救助流程衔接、领养与回访衔接、 捐赠确认与公示、志愿者协作)
java·前端·javascript·人工智能·mysql·echarts·推荐算法
仰望.8 小时前
vue 表格组件 vxe-table 树表格懒加载支持拖拽排序的详细用法
前端·javascript·vue.js·vxe-table
前端 贾公子8 小时前
vite-plugin-uni-manifest 升级指南
前端
小码哥_常8 小时前
iPhone 用户传了张 HEIC,我的前端崩了
前端
小码哥_常8 小时前
你发的原图可能暴露你家地址——聊聊图片 EXIF 隐私
前端
java1234_小锋8 小时前
【技术专题】CSS3 网页技术 - CSS3 优先级
前端·css·css3
鸿蒙开发10 小时前
共享 Schema 与 SchemaFactory
前端·后端·harmonyos
屏什么录10 小时前
录屏超过两小时就卡顿崩盘?从参数调优到代码实现,一文彻底搞定长时间录屏
前端·音视频开发