简体   繁体   English

CSS 仅允许在元素内选择文本

[英]CSS allow text selection within an element only

Here I am trying to achieve where user can select text within an element only, eg: textfield or textarea In these if we have text in textfield then its allowed only to select text within a textfield area, its wont select outer text.在这里,我试图实现用户可以仅在元素内使用 select 文本的位置,例如: textfieldtextarea在这些中,如果我们在 textfield 中有文本,那么它只允许在 textfield 区域内使用 select 文本,它不会 Z999382944282F04016EF8 外部文本How can we do this in the case of div tag?div标签的情况下我们如何做到这一点? any help will be appreciated.任何帮助将不胜感激。

<div>
    <div id="one"> some content will be here... </div>
    <div id="two"> text should be select from this div only.. </div>
</div>

Apply the style user-select: none on #one like this:#one上应用样式user-select: none ,如下所示:

 #one { user-select: none; }
 <div> <div id="one"> some content will be here... </div> <div id="two"> text should be select from this div only.. </div> </div>


You can also put user-select: none on the parent and apply it back to specific children within the parent to make them selectable.您还可以将user-select: none放在父级上并将其应用回父级中的特定子级以使它们可选择。

 .no-user-select { user-select: none; }.has-user-select { user-select: text; }
 <div class="no-user-select"> <div id="one"> some content will be here... </div> <div id="two" class="has-user-select"> text should be select from this div only.. </div> <div id="three"> cannot select from this child either </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM