Javascript Firefox无法将焦点设置为文本区域

Javascript Firefox无法将焦点设置为文本区域,javascript,firefox,textarea,paste,execcommand,Javascript,Firefox,Textarea,Paste,Execcommand,我有一个脚本,在其中捕获用户粘贴数据,对其进行操作,然后将其插入到用户光标所在的位置。这是因为粘贴数据是HTML,用户粘贴的位置必须是textarea,它只接受纯文本。代码如下: <textarea autofocus="true" id="editable" name="editable"></textarea> 稍后,在执行了必要的转换之后,我尝试检索它: editable.focus(); editable.setSelectionRange(st

我有一个脚本,在其中捕获用户粘贴数据,对其进行操作,然后将其插入到用户光标所在的位置。这是因为粘贴数据是HTML,用户粘贴的位置必须是
textarea
,它只接受纯文本。代码如下:

<textarea autofocus="true" id="editable" name="editable"></textarea>
稍后,在执行了必要的转换之后,我尝试检索它:

    editable.focus();
    editable.setSelectionRange(storedCaret, storedCaret);
    window.setTimeout(function() {
        document.execCommand("insertHTML", false, formattedText);
    }, 50);
这对于铬合金(49.0.2623.87米)来说效果很好。但让我非常沮丧的是,在Firefox(43.0.1)中,粘贴不起作用。根据我的调试,在execCommand激发时,
textarea#editable
被正确选择为
document.activeElement
,但粘贴完全失败


jQuery是一个可以接受的解决方案,但我不想使用任何其他PLGUIN。

对于处于我这个位置的迷失和困惑的灵魂,这就是我最终要做的:

if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
        {
            var strLeft = textarea.value.substring(0, currentPos);
            var strRight = textarea.value.substring(currentPos, textarea.value.length);
            textarea.value = strLeft + formattedText + strRight;
        }
它被包裹在if中,因为insertHTML在Chrome中工作得更好。它基本上保存光标位置,然后在字符串中拼接。野蛮但对我的需求有效

if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
        {
            var strLeft = textarea.value.substring(0, currentPos);
            var strRight = textarea.value.substring(currentPos, textarea.value.length);
            textarea.value = strLeft + formattedText + strRight;
        }