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);
    },
相关推荐
恋猫de小郭5 小时前
Flutter 状态管理基准测评,一个很有趣的观点
android·前端·flutter
雪芽蓝域zzs5 小时前
前端编辑组件wangEditor
前端
风骏时光牛马5 小时前
稳定性治理及疑难问题深度剖析与实战复盘
前端
AIGC小尼5 小时前
穿山甲 + 腾讯短剧短视频聚合广告平台|Android+SpringBoot+Vue+Docker 完整部署指南
android·vue.js·spring boot·聚合广告·广告平台·穿山甲广告·腾讯广告
JeffongTan13 小时前
在LWC中镶嵌VF Page获取用户IP
前端·javascript·salesforce
陆枫Larry16 小时前
Astro 是什么
前端
是立不是利17 小时前
写给设计师的 CSS 博客美学指南
前端·css
默_笙18 小时前
🏝 Docker 就是"房地产开发":从施工图纸到小区物业的容器化指南
前端·javascript
计算机魔术师18 小时前
Rohan Paul 谈用时间跨度衡量智能体能力,并引用 OpenAI 自动化研究实习生里程碑
前端
kyriewen18 小时前
我手写了个极简版 React Router——才搞懂 v6 为什么砍了这么多 API
前端·javascript·程序员