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);
    },
相关推荐
IT_陈寒8 小时前
Redis那个过期删除策略,差点让我线上崩了
前端·人工智能·后端
jianqiang.xue8 小时前
Espressif-IDE进入维护模式:乐鑫把ESP32开发的宝押给VS Code
前端·css·ide
特创数字科技8 小时前
纯本地离线运行的文档脱敏工具(技术分享)
前端·驱动开发·python
心之语歌9 小时前
uni-app uts原生插件开发:uts 与 Kotlin 交互踩坑实录(附解决方案)
前端·后端
再吃一根胡萝卜9 小时前
DSH 插件数据链路:Host 管数据,Client 只管画
前端
feixing_fx9 小时前
CSS 变量(Custom Properties):构建可扩展的主题切换系统
前端·css·前端框架·html·css3
十有八七10 小时前
手搓 Agent(一):先有大脑——大模型只会做一件事
前端·人工智能·后端
锋行天下10 小时前
「踩坑实录」给 LangGraph Agent 接两个 FastMCP,我被 langchain 偷偷改了工具名
前端·人工智能·后端
跳跳的小星星11 小时前
自研组件】Vue2 + ElementUI 通用列表页封装 wkjp-list-page,一套配置搞定搜索、动态表格、分页、增删改
前端
tryCbest11 小时前
Vue3—— 第 18 篇:Vue 2 到 Vue 3 迁移指南——升级避坑全记录
前端·vue.js