当我在 <input type="number">
上突出显示数字时在 Chrome 中,window.getSelection().toString()
成功地给了我突出显示的文本。
但在 Firefox 中并非如此。它总是空白的。有谁知道为什么?这真是令人困惑,因为 MDN getSelection documentation声明它应该在 Firefox 57 中工作。
最佳答案
这是一个火狐的错误。见 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;
}
}
关于javascript - window.getSelection().toString() 在 Firefox 中不起作用(在 Chrome 中起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47517432/