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工作流
一个迷人的女人ComfyUI工作流
一只外星甲壳虫子ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

SVG路径动画效果
利用CSS3代码编写45款按钮效果
利用css绘画棋盘布局(象棋)
css3结合svg做一个动态广告
制作一个好玩的倒计时
css3做一个风雨雷电天气动态图标
css3动画loading效果










