vue指令实现、jquery两种方式实现一键复制功能

1、vue指令实现意见复制功能

点击复制会将input中内容复制到粘贴板

 html

<input type="text" v-model="currentQueBasicInfo.requestUrl" readonly />

<span class="btn btn-primary" v-copy="currentQueBasicInfo.requestUrl">复制</span>

javascript 

directives: {
            copy: {
                bind(el, {value}) {
                    el.$value = value; // 用一个全局属性来存传进来的值,因为这个值在别的钩子函数里还会用到
                    el.handler = () => {
                        if (!el.$value) {
                            // 值为空的时候,给出提示,我这里的提示是用的 ant-design-vue 的提示,你们随意
                            console.log('无复制内容');
                            return;
                        }
                        // 动态创建 textarea 标签
                        const textarea = document.createElement('textarea');
                        // 将该 textarea 设为 readonly 防止 iOS 下自动唤起键盘,同时将 textarea 移出可视区域
                        textarea.readOnly = 'readonly';
                        textarea.style.position = 'absolute';
                        textarea.style.left = '-9999px';
                        // 将要 copy 的值赋给 textarea 标签的 value 属性
                        textarea.value = el.$value;
                        // 将 textarea 插入到 body 中
                        document.body.appendChild(textarea);
                        // 选中值并复制
                        textarea.select();
                        textarea.setSelectionRange(0, textarea.value.length);
                        const result = document.execCommand('Copy');
                        if (result) {
                            alertify.alert("复制成功!");
                        }
                        document.body.removeChild(textarea);
                    };
                    // 绑定点击事件,就是所谓的一键 copy 啦
                    el.addEventListener('click', el.handler);
                },
                // 当传进来的值更新的时候触发
                componentUpdated(el, {value}) {
                    el.$value = value;
                },
                // 指令与元素解绑的时候,移除事件绑定
                unbind(el) {
                    el.removeEventListener('click', el.handler);
                }
            }
        },

2、jquery版本:手机、pc通用

效果图如下:

页面如下
复制页面展示如下
复制后粘贴到控制台效果
复制后粘贴到控制台效果

 

 

html:

<div id="target">

    sudo xattr -d com.apple.quarantine /Applications/Wpspaster.app
     
    <span class="iconfont icon-fuzhi" id="copy-btn" title="复制"></span>

</div>
css:        

        #target {
            padding: 16px;
            overflow: auto;
            font-size: 16px;
            background-color: #f6f8fa;
            border-radius: 3px;
            margin: 0;
            position: relative;
        }

        #target:hover #copy-btn {
            opacity: 1;
        }

        #copy-btn {
            font-size: 14px;
            width: 24px;
            height: 24px;
            line-height: 24px;
            text-align: center;
            border-radius: 3px;
            border: 1px solid #dce3e8;
            background: #f6f8fa;
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #8c92a4;
            opacity: 0;
            -webkit-transition: 0.3s;
            -moz-transition: 0.3s;
            -ms-transition: 0.3s;
            -o-transition: 0.3s;
            transition: 0.3s;
        }

        #copy-btn:hover {
            color: #000000;
        }

 

javascript:

$(document).ready(function () {

        $('#copy-btn').on('click', handleCopy)

        function handleCopy() {
            // 创建range对象
            const range = document.createRange();
            
            //获取复制内容的 id 选择器
            range.selectNode(document.getElementById('target'));

            //创建 selection对象
            const selection = window.getSelection(); 
 
            //如果页面已经有选取了的话,会自动删除这个选区,没有选区的话,会把这个选取加入选区
            if (selection.rangeCount > 0) selection.removeAllRanges(); 

            selection.addRange(range); //将range对象添加到selection选区当中,会高亮文本块
            document.execCommand('copy'); //复制选中的文字到剪贴板

            $('.copySuccess').addClass('showCopySuccess')

            setTimeout(() => {
                selection.removeRange(range); // 移除选中的元素
                $('.copySuccess').removeClass('showCopySuccess')

            }, 1500)
        }

 })

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 可以通过自定义指令 (Directive) 和 Marked 库来实现代码一键复制功能。 下面是实现的基本步骤: 1. 安装 Marked 库 ```bash npm install marked ``` 2. 创建一个自定义指令Vue 3 中,自定义指令可以通过 `app.directive` 方法来创建。下面是一个创建 `copy-code` 指令的示例代码: ```javascript import marked from 'marked'; const copyCodeDirective = { mounted(el, binding) { const code = el.querySelector('code'); const button = document.createElement('button'); button.innerText = 'Copy'; button.addEventListener('click', () => { navigator.clipboard.writeText(code.textContent.trim()); button.innerText = 'Copied!'; setTimeout(() => { button.innerText = 'Copy'; }, 2000); }); el.appendChild(button); }, updated(el) { const code = el.querySelector('code'); const html = marked(code.textContent); code.innerHTML = html; } }; export default copyCodeDirective; ``` 这个自定义指令会在绑定的元素上添加一个 "Copy" 按钮,并且当用户点击按钮时,会将元素内部的代码文本复制到剪贴板中。 3. 在组件中使用自定义指令 ```html <template> <pre v-copy-code><code>{{ code }}</code></pre> </template> <script> import copyCodeDirective from './copyCodeDirective.js'; export default { directives: { copyCode: copyCodeDirective }, data() { return { code: 'console.log("Hello, world!");' } } }; </script> ``` 在组件中,我们可以将 `v-copy-code` 指令绑定到 `<pre>` 标签上,然后将代码文本插入到 `<code>` 标签中。 当我们在浏览器中运行这个组件时,就会看到代码旁边出现一个 "Copy" 按钮。当我们点击该按钮时,代码文本就会被复制到剪贴板中。 希望这个示例能够帮助您实现您的功能

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值