【问题标题】:window.getSelection().toString() not working in Firefox (works in Chrome)window.getSelection().toString() 在 Firefox 中不起作用(在 Chrome 中起作用)
【发布时间】:2017-11-27 18:22:01
【问题描述】:

当我在 Chrome 中突出显示 <input type="number"> 上的数字时,window.getSelection().toString() 成功地为我提供了突出显示的文本。

但在 Firefox 中并非如此;它总是空白的。有谁知道为什么?这真的很令人困惑,因为 MDN getSelection documentation 声明它应该在 Firefox 57 中工作。

【问题讨论】:

  • 你在哪里运行你的代码?
  • “空白”是指空字符串?我在这个页面上测试它工作正常。这只发生在特定页面上吗?
  • 您能否提供您正在使用的代码示例以及预期输出?
  • 非常奇怪的错误。我可以修复它的宽度输入文本,但不能修复数字。

标签: javascript firefox mozilla


【解决方案1】:

这是一个 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;
            }
        }        

【讨论】:

猜你喜欢
  • 2013-12-23
  • 2012-12-26
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多