HTML DOM 样式的 userSelect 属性返回并修改用户是否可以在 HTML 文档中选择元素的文本。
用法
以下是语法 -
返回用户选择
object.style.userSelect
修改用户选择
object.style.userSelect = “value”
值
在这里,价值可以是 -
值 | 解释 |
---|---|
auto | 它允许用户根据浏览器设置选择文本。 |
none | 它不允许用户选择文本。 |
text | 用户可以在其中选择文本。 |
all | 它允许通过单击而不是双击来选择文本。 |
示例
让我们看一个 HTML DOM 样式 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>
输出
这将产生以下输出 -
点击 ”Set userSelect” 按钮将 userSelect 设置为 none 作为段落元素的值。
相关用法
- HTML DOM Style unicodeBidi属性用法及代码示例
- HTML DOM Style overflowY属性用法及代码示例
- HTML DOM Style pageBreakAfter属性用法及代码示例
- HTML DOM Style transition属性用法及代码示例
- HTML DOM Style outlineOffset属性用法及代码示例
- HTML DOM Style maxWidth属性用法及代码示例
- HTML DOM Style textAlignLast属性用法及代码示例
- HTML DOM Style borderBottomWidth属性用法及代码示例
- HTML DOM Style width属性用法及代码示例
- HTML DOM Style margin属性用法及代码示例
- HTML DOM Style textDecoration属性用法及代码示例
- HTML DOM Style borderCollapse属性用法及代码示例
- HTML DOM Style backgroundClip属性用法及代码示例
- HTML DOM Style animationIterationCount属性用法及代码示例
- HTML DOM Style animationFillMode属性用法及代码示例
- HTML DOM Style pageBreakInside属性用法及代码示例
- HTML DOM Style paddingTop属性用法及代码示例
- HTML DOM Style textDecorationColor属性用法及代码示例
- HTML DOM Style animation属性用法及代码示例
- HTML DOM Style transitionDelay属性用法及代码示例
注:本文由纯净天空筛选整理自AmitDiwan大神的英文原创作品 HTML DOM Style userSelect Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。