Html contentEditable-execCommand';标题';仅在Firefox中工作

Html contentEditable-execCommand';标题';仅在Firefox中工作,html,contenteditable,execcommand,html-heading,Html,Contenteditable,Execcommand,Html Heading,我有以下代码,它将所选文本包装在H2标记中 问题是,它只在Firefox中有效(尽管我还没有在IE中测试过)。它在Safari或Chrome中不起作用 我是否错过了一些东西,或者这个功能不兼容所有浏览器 <button onclick="document.execCommand('heading',false,'h2');">heading</button> <div class="editable" contentEditable> <p&g

我有以下代码,它将所选文本包装在H2标记中

问题是,它只在Firefox中有效(尽管我还没有在IE中测试过)。它在Safari或Chrome中不起作用

我是否错过了一些东西,或者这个功能不兼容所有浏览器

<button onclick="document.execCommand('heading',false,'h2');">heading</button>

<div class="editable" contentEditable>
    <p>Add content here...</p>
</div>
标题
在这里添加内容


[注意:
document.execCommand
及其功能现在得到广泛支持。]

您可能已经找到了答案,但由于我遇到了相同的问题,我将发布以下内容:

这不是一项广泛支持的功能。 如图所示:

似乎您必须使用formatBlock:

document.execCommand('formatBlock', false, '<'+dataValue+'>');  //dataValue must be h2 in your case
document.execCommand('formatBlock',false',)//在您的情况下,数据值必须为h2

要创建H2标记,您需要使用

document.execCommand('formatBlock',false,'h2')
它会起作用的