vuejs如何实现复制粘贴功能,下面web建站小编给大家详细介绍一下实现代码!
新建copy.js的文件
const vCopy = {
bind(el, { value }) {
el.$value = value;
el.handler = () => {
if (!el.$value) {
console.log('无复制内容');
return;
}
const textarea = document.createElement('textarea');
textarea.readOnly = 'readonly';
textarea.style.position = 'absolute';
textarea.style.left = '-9999px';
textarea.value = el.$value;
document.body.appendChild(textarea);
// 选中值并复制
textarea.select();
textarea.setSelectionRange(0, textarea.value.length);
const result = document.execCommand('Copy');
if (result) {
console.log('复制成功');
}
document.body.removeChild(textarea);
};
el.addEventListener('click', el.handler);
},
componentUpdated(el, { value }) {
el.$value = value;
},
unbind(el) {
el.removeEventListener('click', el.handler);
},
};
export default vCopy;
新建directives.js文件
import copy from './copy.js';
// 自定义指令
const directives = {
copy,
};
export default {
install(Vue) {
Object.keys(directives).forEach((key) => {
Vue.directive(key, directives[key]);
});
},
};
main.js全局引入
import Vue from 'vue'; import Directives from './directives'; Vue.use(Directives);
页面调用
<template>
<div >
<button v-copy="copyText">拷贝</button>
</div>
</template>
<script>
export default {
data(){
return {
copyText:'要copy的内容'
}
},
methods: {
init () {
},
},
mounted () {
_this = this;
_this.init();
},
}
</script>
上面是“vuejs如何实现复制粘贴功能”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_3057.html
workflows工作流
一碗热气腾腾的拉面ComfyUI工作流
一只可爱的快乐老鼠戴着帽子ComfyUI工作流
一只穿着黑色蝴蝶结西装可爱橙色小猫
一个时间旅行女人的神秘剪影ComfyUI工作流
一个戴着破旧莎草帽的机械忍者
一个可爱的人形小机器人和一只蜗牛在森林
一桌精致的美食,桌上几杯白葡萄酒
斗鸡场威武雄鸡ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

黑客入侵效果代码
css3搭积木叠加图形
利用css3做一个动态loading效果
css3做一个风雨雷电天气动态图标
用svg画出游泳池动画效果
jquery鼠标滑过图片边框特效(jquery.focus-follow插件)
会议人员60s签到倒计时插件
纯css制作卡通头像(随鼠标转头)











