contentEditable - execCommand'heading'仅适用于Firefox

Tom*_*Tom 3 html contenteditable execcommand html-heading

我有以下代码将选定的文本包装在H2标签中.

问题是,它只适用于Firefox(虽然我还没有在IE中测试过).它在Safari或Chrome中不起作用.

我错过了什么或者这个功能是否与所有浏览器不兼容?

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

<div class="editable" contentEditable>
    <p>Add content here...</p>
</div>
Run Code Online (Sandbox Code Playgroud)

Mic*_*lix 10

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

你可能已经找到了答案,但由于我遇到了同样的问题,我会发布这个:

它不是广泛支持的功能. 如下所示:http: //www.quirksmode.org/dom/execCommand.html#t013

看来你必须使用formatBlock:

document.execCommand('formatBlock', false, '<'+dataValue+'>');  //dataValue must be h2 in your case
Run Code Online (Sandbox Code Playgroud)