[英]How to clear text selection in Mobile Safari with JavaScript?
Is there a way to programmatically clear a selection box in Mobile Safari? 有没有办法以编程方式清除Mobile Safari中的选择框? I tried
window.getSelection().removeAllRanges()
as suggested by the answers to another Stack Overflow question, Clear Text Selection with JavaScript , and while the Selection object is reset to no selection (type is "None", isCollapsed is true
, rangeCount is 0, and the anchorNode
/ focusNode
/ baseNode
/ extentNode
are null
), the selection box remains on screen: 我按照另一个Stack Overflow问题的答案, 使用JavaScript清除文本选择的建议尝试了
window.getSelection().removeAllRanges()
,当Selection对象重置为无选择时(类型为“None”,isCollapsed为true
,rangeCount为0,并且anchorNode
/ focusNode
/ baseNode
/ extentNode
为null
),选择框保留在屏幕上:
I also tried window.getSelection().collapse()
, but that did not work. 我也尝试过
window.getSelection().collapse()
,但是没有用。
I am testing iOS 7.1 Simulator as well as Mobile Safari on an iPad running iOS 7.1.1. 我在运行iOS 7.1.1的iPad上测试iOS 7.1模拟器和Mobile Safari。
This has been a known bug in Mobile Safari since 2010! 自2010年以来,这一直是Mobile Safari中的一个已知错误! http://www.openradar.me/8707236
http://www.openradar.me/8707236
The year is now 2018 and this is still a bug. 这一年现在是2018年,这仍然是一个错误。 (iOS 10.2.2)
(iOS 10.2.2)
In areas where it was not needed, removing event.preventDefault()
helped. 在不需要它的区域,删除
event.preventDefault()
帮助。
I was having the exact same problem and in my case what made a difference was removing 我有完全相同的问题,在我的情况下,有什么不同是删除
event.preventDefault();
from the handler of the "deselection" tap event. 来自“取消选择”点击事件的处理程序。
One possibility is to quickly add and remove the user-select
CSS class of the parent element, to disable and enable the user selection. 一种可能性是快速添加和删除父元素的
user-select
CSS类,以禁用和启用用户选择。
For example, 例如,
parent = getSelection().anchorNode.parentNode
parent.style.userSelect = "none"
setTimeout(()=>parent.style.userSelect = <default>, 100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.