![](/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.