有没有一种方法可以通过双击不复制文本?

时间:2019-06-13 09:05:52

标签: javascript html

我不希望在我双击时对文本进行标记

有什么办法不能发生?

我没有找到任何东西,所以我无法尝试任何东西。


  

该代码仅用于防止在按住鼠标左键的同时进行复制

<body oncopy="return false" oncut="return false" onpaste="return false">

2 个答案:

答案 0 :(得分:1)

在CSS中使用user-select: none;

.unselectable {
  -webkit-user-select: none;  /* Chrome all / Safari all */
  -moz-user-select: none;  /* Firefox all */
  -ms-user-select: none;  /* IE 10+ */
  user-select: none;
}
<div class="unselectable">Cant highlight this</div>

您还可以在将规则应用于所有子项的父容器(例如body)上使用它,只要子项不修改user-select

.unselectable {
  -webkit-user-select: none;
  /* Chrome all / Safari all */
  -moz-user-select: none;
  /* Firefox all */
  -ms-user-select: none;
  /* IE 10+ */
  user-select: none;
}
<body class="unselectable">
  <div>Cant highlight this</div>
  Or this
  <p>Or any of this</p>
</body>

答案 1 :(得分:0)

使用user-select属性-但请确保添加所有不同的浏览器/引擎前缀。

.no-select {
  -webkit-user-select: none; /* Webkit engine for Safari and Chrome */
  -moz-user-select: none;    /* Mozilla engine for Firefox */
  -ms-user-select: none;     /* Microsoft browsers */
  user-select: none;         /* Generic property usage */
}
<p class="no-select">Unselectable</p>

相关问题