HTML DOM Style userSelect 属性
htmlweb developmentfront end technology更新于 2025/6/11 9:16:17
HTML DOM Style userSelect 属性返回并修改 HTML 文档中元素的文本是否可由用户选择。
语法
以下是语法 −
返回 userSelect
object.style.userSelect
修改 userSelect
object.style.userSelect = “value”
值
此处,值可以为 -
| 值 | 说明 |
|---|---|
| auto | 允许用户根据浏览器设置选择文本。 |
| none | 不允许用户选择文本。 |
| text | 用户可以在其中选择文本。 |
| all | 允许单击而非双击选择文本。 |
示例
让我们看一个 HTML DOM Style userSelect 属性 −
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
background: lightblue;
height: 100vh;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
}
.show {
margin: 1rem 0;
}
</style>
</head>
<body>
<h1>DOM Style userSelect Property Example</h1>
<p style="direction:ltr">This is a paragraph element with some dummy text.</p>
<button onclick="add()" class="btn">Set userSelect</button>
<div class="show"></div>
<script>
function add() {
document.querySelector('p').style.userSelect = "none";
document.querySelector('.show').innerHTML = "Now you can't select the above paragraph text";
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"设置 userSelect"按钮,将段落元素上的 userSelect 设置为 none 值。

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

