document.getSelection() 和 window.getSelection() 函数在 iOS 12 中不起作用。
该问题在 Safari、Google Chrome 和 WKWebView 中重现。
在 iOS 11 和 MacOS(任何版本)中,这些功能都有效。
需要任何想法如何获取用户的文本选择。
document.querySelector("#contentjs").onclick = function () {
console.log(document.getSelection());
document.querySelector("#result").innerHTML = document.getSelection().anchorOffset;
}
该问题的解决方案是使用 document.caretRangeFromPoint 函数
例子: