简体   繁体   English

我有contenteditable div,并且在它内部是一个不可编辑的span,如何能够通过长按在整个span文本上标记div的文本

[英]I have contenteditable div , and inside it is a noneditable span, how to be able to mark the div's text with a long click thrugh the span text

I have contenteditable div , and inside it is a noneditable span,what should I do to be able to select the div's text with a long swing click starting on the the span text, and stop at the text I want to select? 我有contenteditable div,并且在它内部是一个不可编辑的跨度,我应该怎么做才能长按一下以选择div的文本,然后单击跨度文本,然后在要选择的文本处停止呢? I want to only select the text inside the div but NOT the text inside the noneditable span 我只想选择div内的文本,而不要选择不可编辑范围内的文本

 body { background: #20262E; padding: 20px; font-family: Helvetica; } span{ color:lightgrey; } #banner-message { background: #fff; border-radius: 4px; padding: 20px; font-size: 25px; text-align: center; transition: all 0.2s; margin: 0 auto; width: 100%; } 
 <div id="banner-message"> <div contenteditable ='true'> some editable text <span class="main-search-placeholder" contenteditable="false" style="-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;" unselectable = "on" onselectstart = "return false;" onmousedown = "return false;"> some other noneditable text </span> </div> </div> 

UPDATE: what i mean is i want to be able to select the text even if my mousedown has been clicked on the span area and then select the amount of characters inside the editabel text 更新:我的意思是即使我在跨度区域上单击鼠标右键也希望能够选择文本,然后选择editabel文本内的字符数

CSS CSS

  • Remove the focus outline of the contentEditable outer div 删除contentEditable外层div的焦点轮廓
  • Surround the editable part with an element 用元素包围可编辑部分
  • When focused in the outer div with contentEditable='true' , show the focus outline on the editable region only 当使用contentEditable='true'聚焦在外部div时,仅在可编辑区域上显示聚焦轮廓

JS JS

  • I set long press to 1 second (adjustable) 我将长按设置为1秒(可调)
  • You will have the exciting task of allowing only long clicks into the element 您将拥有令人激动的任务,即允许长时间单击该元素
  • To test it, click anywhere on the outer div ( #banner-message ) and you should see the focus on only the editable part of the text 要对其进行测试,请单击外部div( #banner-message )上的任意位置,您应该会看到仅关注文本的可编辑部分
[contenteditable ='true']:focus {
  outline: none;
}

[contenteditable ='true'] .editable {
  display: inline-block;
}

[contenteditable ='true']:focus .editable {
  outline: 1px dotted gray;
}

Gif GIF

At the beginning of the gif , I'm clicking on the non-editable part of the content and nothing happens. gif的开头,我单击了内容的不可编辑部分,没有任何反应。 Then, at the end of the gif , I'm long-clicking on the outer div to show selection of text is working in the proper place. 然后,在gif的末尾,我长按外部div以显示在适当的位置选择的文本。

在此处输入图片说明

Demo: 演示:

 var pressTimer; var bannerMessage = document.getElementById("banner-message"); var editableRegion = bannerMessage.querySelector("[contenteditable ='true'] .editable"); function handleMouseUp() { clearTimeout(pressTimer); return false; } function handleMouseDown() { pressTimer = window.setTimeout(function() { if (document.body.createTextRange) { const range = document.body.createTextRange(); range.moveToElementText(editableRegion); range.select(); } else if (window.getSelection) { var selection = window.getSelection(); var range = document.createRange(); range.selectNodeContents(editableRegion); selection.removeAllRanges(); selection.addRange(range); } }, 1000); return false; } bannerMessage.addEventListener("mouseup", handleMouseUp); bannerMessage.addEventListener("mousedown", handleMouseDown); 
 body { background: #20262E; padding: 20px; font-family: Helvetica; } span { color: lightgrey; } #banner-message { background: #fff; border-radius: 4px; padding: 20px; font-size: 25px; text-align: center; transition: all 0.2s; margin: 0 auto; width: 100%; outline: none; } [contenteditable='true']:focus { outline: none; } [contenteditable='true'] .editable { display: inline-block; } [contenteditable='true']:focus .editable { outline: 1px dotted gray; } 
 <div id="banner-message"> <div contenteditable='true'> <div tabindex="-1" class="editable">some editable text</div> <span class="main-search-placeholder" contenteditable="false" style="-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;" unselectable="on" onselectstart="return false;" onmousedown="return false;"> some other noneditable text </span> </div> </div> 

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

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