最近需要用的禁用复制功能,给大家分享两种禁止复制的方法。
1.使用js来实现:
//在vue 2.x中实现
mounted() {
// 禁用复制
this.$nextTick(() => {
document.onselectstart = new Function("event.returnValue=false");
})
}
//在vue 3.0中实现
import {
onMounted } from 'vue';
onMounted(() => {
// 禁用复制
document.onselectstart = new Function("event.returnValue=false");
})
2.使用css来实现:
/*
user-select: auto|none|text|all;
auto 默认。如果浏览器允许,则可以选择文本。
none 防止文本选取。
text 文本可被用户选取。
all 单击选取文本,而不是双击。
*/
//全局选择
*{
user-select:none;
}
//或者部分标签
.prohibitSel{
user-select:none;
}