JavaScript:通过双击禁用文本选择
当双击 html 页面时,大多数浏览器都会选择您双击的单词(或三次单击的段落)。有没有办法摆脱这种行为?
请注意,我不想通过单击+拖动来禁用常规选择;即 jQuery UI 的 $('body').disableSelection()
和 document.onselectstart
DOM 事件不是我想要的。
When double-clicking on a html page most browsers select the word you double-click on (or the paragraph you triple-click on). Is there a way to get rid of this behavior?
Note that I do not want to disable regular selection via single-click+dragging; i.e. jQuery UI's $('body').disableSelection()
and the document.onselectstart
DOM event are not what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我担心您无法阻止选择本身成为浏览器的“本机行为”,但您可以在选择后立即清除选择:
编辑:还可以防止通过“三次单击”选择整个段落,这是所需的代码:
实时测试用例。
应该是跨浏览器的,请报告任何无法正常工作的浏览器。
I fear you can't prevent the selection itself being "native behavior" of the browser, but you can clear the selection right after it's made:
Edit: to also prevent selecting whole paragraph by "triple click", here is the required code:
Live test case.
Should be cross browser, please report any browser where it's not working.
以下内容适用于当前的 Chrome (v56)、Firefox (v51) 和 MS Edge (v38) 浏览器。
MouseEvent.detail 属性跟踪 当前点击计数,可用于确定事件是两次、三次还是多次点击。
遗憾的是,Internet Explorer 在超时后不会重置计数器,因此您不会获得突发点击次数,而是会获得自页面加载以来用户点击次数的计数。
The following works for me in the current Chrome (v56), Firefox (v51) and MS Edge (v38) browsers.
The MouseEvent.detail property keeps track of the current click count which can be used to determine whether the event is a double, tripple, or more click.
Internet explorer unfortunately does not reset the counter after a timeout period so instead of getting a count of burst-clicks you get a count of how many times the user has clicked since the page was loaded.
只需将其放在 css 感兴趣的部分即可
Just put this on the css interested section
如果您确实想在双击时禁用选择,而不仅仅是随后删除选择(对我来说看起来很难看),则需要在第二个 mousedown 事件中
返回 false
( ondblclick 不起作用,因为选择是在 mousedown 上进行的)。**如果有人根本不想选择,最好的解决方案是使用 CSS
user-select : none;
就像 Maurizio Battaghini 提出的那样。现场演示
重要通知:我将灵敏度设置为 500 毫秒,但双击灵敏度(检测为双击的两次单击之间的最长时间)可能会因操作系统和浏览器的不同而有所不同,并且通常是用户可配置的。 - api.jquery.com
在 Chrome 和 IE11 中测试。
If you really want to disable selection on double-click and not just remove the selection afterwards (looks ugly to me), you need to
return false
in the second mousedown event (ondblclick won't work because the selection is made onmousedown).**If somebody wants no selection at all, the best solution is to use CSS
user-select : none;
like Maurizio Battaghini proposed.Live demo here
Important notice: I set the sensitivity to 500ms but Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable. - api.jquery.com
Tested in Chrome and IE11.
只是为了把这个扔出去,但这是我放入页面的代码,我希望用户能够快速点击。但是,这也会禁用标准的单击拖动文本选择。
似乎对我来说效果很好。
Just to throw this out there, but here's the code I slap into my pages where I expect users to be clicking rapidly. However, this will also disable standard click-n-drag text selection.
Seems to work well for me.