Recently, I need to use the function of disabling copying, and I will share with you two methods of prohibiting copying.
1. Use js to achieve:
//在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. Use css to achieve:
/*
user-select: auto|none|text|all;
auto 默认。如果浏览器允许,则可以选择文本。
none 防止文本选取。
text 文本可被用户选取。
all 单击选取文本,而不是双击。
*/
//全局选择
*{
user-select:none;
}
//或者部分标签
.prohibitSel{
user-select:none;
}