![](/img/trans.png)
[英]window.getSelection().toString() not working on IE 11
[英]window.getSelection().toString() not working in Firefox (works in Chrome)
当我在Chrome中的<input type="number">
上突出显示数字时, window.getSelection().toString()
成功地为我提供了突出显示的文本。
但在Firefox中并非如此; 它总是空白的。 有谁知道为什么? 这实在令人困惑,因为MDN getSelection文档声明它应该在Firefox 57中运行。
这是一个firefox错误。 请参阅https://bugzilla.mozilla.org/show_bug.cgi?id=85686
非常古老,尚未修复。
我使用以下代码作为解决方法:
function getSelectionText() {
if (window.getSelection) {
try {
var activeElement = document.activeElement;
if (activeElement && activeElement.value) {
// firefox bug https://bugzilla.mozilla.org/show_bug.cgi?id=85686
return activeElement.value.substring(activeElement.selectionStart, activeElement.selectionEnd);
} else {
return window.getSelection().toString();
}
} catch (e) {
}
} else if (document.selection && document.selection.type != "Control") {
// For IE
return document.selection.createRange().text;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.