使用JavaScript中的userSelect属性可以启用或禁用文本选择。对于Firefox,请使用MozUserSelect属性并将其设置为none,以禁用选择。
您可以尝试运行以下代码来设置是否可以使用JavaScript选择元素的文本-
<!DOCTYPE html>
<html>
<body>
<button onclick = "myFunction()">Click me</button>
<div id = "box">
Click the above button. This won't allow you to select this text. Shows ths usage of userSelect property.
</div>
<script>
function myFunction() {
var a = document.getElementById("box");
a.style.userSelect = "none";
//适用于Chrome和Safari-
a.style.WebkitUserSelect = "none";
//在Firefox中工作
a.style.MozUserSelect = "none";
}
</script>
</body>
</html>