首页 > 解决方案 > 带有用户选择的Div:嵌套在用户选择中时自动不起作用:无

问题描述

我正在使一个 div 不可选择,并且我想在一些嵌套的 div 中重新启用选择。

<div style="user-select: none; -ms-user-select: none; color: red;">
  <div id="noSelection">
    Should not be able to select this text
  </div>
  <div id="selectIt" style="user-select: auto; -ms-user-select:auto; color: green;">
    Wanna select this text as normal with correct mouse cursor
  </div>
</div>

我希望在设置了“自动”的最后一个 div 中正常选择文本,但它在我尝试的任何浏览器中都不起作用。

标签: htmlcss

解决方案


而不是user-select: auto;用于user-select: text;使文本可选。正如使用时的规范所述auto

如果user-select此元素的父元素上 none的计算值为 ,则计算值为none

<div style="user-select: none; -ms-user-select: none; color: red;">
  <div id="noSelection">
    Should not be able to select this text
  </div>
  <div id="selectIt" style="user-select: text; -ms-user-select:auto; color: green;">
    Wanna select this text as normal with correct mouse cursor
  </div>
</div>


推荐阅读