Javascript 选中所有文本时,Firefox中的execCommand bold失败

Javascript 选中所有文本时,Firefox中的execCommand bold失败,javascript,firefox,wysiwyg,contenteditable,execcommand,Javascript,Firefox,Wysiwyg,Contenteditable,Execcommand,我正准备使用JavaScript进行一些简单的所见即所得编辑,但我在Firefox中遇到了一个Chrome或IE(所有浏览器的最新版本)中都没有遇到的问题。当选择mycontentEditablespan中的所有文本时,如果我尝试使用document.execCommand('bold',false,null)将其加粗,我会收到一条非常不寻常的错误消息:“NS\U error\U FAILURE:FAILURE” 下面是一些简单的示例代码,可以轻松重现问题: <html> &

我正准备使用JavaScript进行一些简单的所见即所得编辑,但我在Firefox中遇到了一个Chrome或IE(所有浏览器的最新版本)中都没有遇到的问题。当选择my
contentEditable
span中的所有文本时,如果我尝试使用
document.execCommand('bold',false,null)
将其加粗,我会收到一条非常不寻常的错误消息:“NS\U error\U FAILURE:FAILURE”

下面是一些简单的示例代码,可以轻松重现问题:

<html>
    <head>
        <script>
            function start(){

                var edit = document.getElementById('edit');
                edit.contentEditable = true;

                var button = document.getElementById('button');
                button.onclick = function(){

                    // Get the editable span
                    var edit = document.getElementById('edit');

                    // Select the contents of the span
                    var range = document.createRange();
                    range.selectNodeContents(edit);
                    var selection = window.getSelection();
                    selection.removeAllRanges();
                    selection.addRange(range);

                    // Make the text bold
                    document.execCommand('bold',false,null);

                }

            }
        </script>
    </head>
    <body onload="start();">
        <span id='edit'>Click on the button</span>
        <button id='button'>Bold It All!</button>
    </body>
</html>

函数start(){
var edit=document.getElementById('edit');
edit.contentEditable=true;
var button=document.getElementById('button');
button.onclick=函数(){
//获取可编辑范围
var edit=document.getElementById('edit');
//选择范围的内容
var range=document.createRange();
范围。选择节点内容(编辑);
var selection=window.getSelection();
selection.removeAllRanges();
选择。添加范围(范围);
//将文本加粗
document.execCommand('bold',false,null);
}
}
点击按钮
大胆一点!
那么,我做错了什么?我是不是刚碰到一只虫子?如果是这样的话,有人能提出解决方案吗?

这是一个bug。考虑简言之:

  • 编辑器将尝试用一个
    (或另一个
    使用CSS
    时)包装
  • 这将删除
  • 因此,
    的父代码是可编辑的,而不是可编辑的

解决方法:
contenteditable=“true”

这样的实际块元素,非常感谢。看起来有人已经拿到了。你能想到其他的方法来帮助这个部门吗?不幸的是,我在一个环境中工作,在这个环境中,另一个我无法控制的脚本要求所讨论的内容必须在其中。