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);
    },
相关推荐
小雨青年8 分钟前
Cursor 项目实战:AI播客策划助手(二)—— 多轮交互打磨播客文案的技术实现与实践
前端·人工智能·状态模式·交互
533_23 分钟前
[vue3] h函数,阻止事件冒泡
javascript·vue.js·elementui
蒲公英源码25 分钟前
php+vue知识付费系统前后端全套源码
vue.js·php
通往曙光的路上26 分钟前
day22_用户授权 头像上传
javascript·vue.js·ecmascript
小阳生煎28 分钟前
Vue实现全局设置一个刷新按钮 只刷新当面路由页面 不跳转操作功能
vue.js·vue
小光学长29 分钟前
基于Vue的儿童手工创意店管理系统as8celp7(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库·vue.js
meichaoWen30 分钟前
【Vue】Vue框架的基础知识强化
前端·javascript·vue.js
jingling55534 分钟前
Flutter | 基础环境配置和创建flutter项目
前端·flutter
mapbar_front37 分钟前
如何判断一个领导值不值得追随
前端
西西学代码39 分钟前
Flutter---DragTarget(颜色拖拽选择器)
前端·javascript·flutter