是否可以将文本选择限制为当前元素? [英] Is it possible to limit text selection to current element?
本文介绍了是否可以将文本选择限制为当前元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<div class="container">
<div class="item1">text text text in div 1</div>
<div class="item2">text text text in div 2</div>
</div>
如果选择是从div.item1中的文本开始的,那么是否有可能(通过任何HTML节点,CSS或JS)阻止选择div.item2中的文本,反之亦然-从div.item2开始并限制为它(防止选择item1表单)?
Is it possible (by any HTML node, CSS or JS) to prevent from selecting text in div.item2 if selection started from text in div.item1 and the other way around - starting from div.item2 and limit to it (prevent item1 form being selected)?
推荐答案
我想到了这个,并附带了一些jQuery代码:
I came up with this, with a bit of jQuery code :
let $items = $(".item")
$items
.mousedown( function() {
$(this).siblings().css({ "user-select" : "none" , color : "lightgrey"})
})
.mouseup( function() {
$items.css({ "user-select" : "auto" , color : "black"})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="item">text text text in div 1</div>
<div class="item">text text text in div 2</div>
<div class="item">text text text in div 3</div>
<div class="item">text text text in div 4</div>
</div>
这篇关于是否可以将文本选择限制为当前元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文