【问题标题】:Prevent Clearance of Selection防止选择清除
【发布时间】:2018-07-27 20:08:02
【问题描述】:

<p>Select Some of This Text</p>
<span>Now click this</span>

是否有可能使当您单击&lt;span&gt; 时它不会删除&lt;p&gt; 的选择或将其存储在某处?也许使用onblur 事件?

【问题讨论】:

  • 您这样做的用例是什么?另外,您能否澄清一下“将其存储在某处”是什么意思?
  • @Nishant 我认为“将其存储在某处”是指在浏览器清除它之前保存文本选择范围的可能解决方案,然后在浏览器完成后立即重新选择该范围选择的设置。这可以使用与inputElement.setSelectionRange 等效的任何非input 元素来完成。 (相当于Range - 请参阅Can you set and/or change the user’s text selection in JavaScript?。)

标签: javascript events dom-events selection


【解决方案1】:

一种方法是使用How to disable text selection highlighting? 中解释的 CSS 来防止在“现在单击此”按钮中选择文本。使用此 CSS,单击按钮不会更改选择,包括是否存在现有选择。

.unselectable {
  -webkit-touch-callout: none;
    -webkit-user-select: none;
     -khtml-user-select: none;
       -moz-user-select: none;
        -ms-user-select: none;
            user-select: none;
}
<p>Select Some of This Text</p>
<span class="unselectable">Now click this</span>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    相关资源
    最近更新 更多