如何使用JavaScript清除Mobile Safari中的文本选择?

时间:2014-05-15 18:05:30

标签: javascript mobile-safari selection

有没有办法以编程方式清除Mobile Safari中的选择框?我按照另一个Stack Overflow问题Clear Text Selection with JavaScript的答案的建议尝试window.getSelection().removeAllRanges(),当Selection对象重置为无选择(类型为“None”,isCollapsed为true时,rangeCount为0,anchorNode / focusNode / baseNode / extentNodenull),选择框仍保留在屏幕上:

Screenshot of the 'Clear Text Selection with JavaScript' question on Stack Overflow, showing the selection box still on screen

我也试过window.getSelection().collapse(),但这没效果。

我正在运行iOS 7.1.1的iPad上测试iOS 7.1模拟器和Mobile Safari。

3 个答案:

答案 0 :(得分:1)

自2010年以来,这已成为Mobile Safari中的一个已知错误! http://www.openradar.me/8707236

这一年现在是2018年,这仍然是一个错误。 (iOS 10.2.2)

在不需要的地方,删除event.preventDefault()有帮助。

答案 1 :(得分:0)

我遇到了完全相同的问题,在我的情况下,有什么不同就是删除

event.preventDefault();
来自" deselection"的处理程序的

点击事件。

答案 2 :(得分:0)

一种可能性是快速添加和删除父元素的user-select CSS类,以禁用和启用用户选择。

例如,

parent = getSelection().anchorNode.parentNode
parent.style.userSelect = "none"
setTimeout(()=>parent.style.userSelect = <default>, 100)