[英]window.getSelection().toString() not working in Firefox (works in Chrome)
When I highlight numbers on an <input type="number">
in Chrome, window.getSelection().toString()
successfully gives me the highlighted text. 当我在Chrome中的
<input type="number">
上突出显示数字时, window.getSelection().toString()
成功地为我提供了突出显示的文本。
But this is not so in Firefox; 但在Firefox中并非如此; it is always blank.
它总是空白的。 Does anyone know why?
有谁知道为什么? This is really confusing since MDN getSelection documentation states it should work in Firefox 57.
这实在令人困惑,因为MDN getSelection文档声明它应该在Firefox 57中运行。
This is a firefox bug. 这是一个firefox错误。 See https://bugzilla.mozilla.org/show_bug.cgi?id=85686
请参阅https://bugzilla.mozilla.org/show_bug.cgi?id=85686
Very old one, not fixed yet. 非常古老,尚未修复。
I use the following code as workaround: 我使用以下代码作为解决方法:
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.