Internet explorer Internet Explorer 11中的formatBlock

Internet explorer Internet Explorer 11中的formatBlock,internet-explorer,formatting,internet-explorer-11,execcommand,Internet Explorer,Formatting,Internet Explorer 11,Execcommand,Internet Explorer中有一个怪癖,当使用execCommand和formatBlock格式化文本时,它迫使您用尖括号括住元素名称。 所有其他浏览器都接受document.execCommand(“formatBlock”,false,“H1”),而Internet Explorer需要document.execCommand(“formatBlock”,false,”) IE10及以下可通过以下技巧检测到: isIE = function () { var userAgen

Internet Explorer中有一个怪癖,当使用execCommand和formatBlock格式化文本时,它迫使您用尖括号括住元素名称。 所有其他浏览器都接受
document.execCommand(“formatBlock”,false,“H1”)
,而Internet Explorer需要
document.execCommand(“formatBlock”,false,”)

IE10及以下可通过以下技巧检测到:

isIE = function () {
    var userAgent   = navigator.userAgent,
        isIE        = userAgent.indexOf("MSIE") !== -1 && userAgent.indexOf("Opera") === -1;

    return isIE;
}
InternetExplorer11假装是Firefox,破坏了浏览器检测。我知道不赞成做浏览器检测,我应该做特征检测。在这种情况下,功能存在,但不一致

是否有其他方法可以在不依赖浏览器检测的情况下一致地应用块格式?

execCommand()
如果命令成功,则返回
true
,否则返回
false
。因此,您可以这样做:

if (!document.execCommand('FormatBlock', null, 'H1')) {
    document.execCommand('FormatBlock', null, '<H1>');
}
if(!document.execCommand('FormatBlock',null,'H1')){
document.execCommand('FormatBlock',null',);
}

尽管其他浏览器似乎也可以使用

您是通过反复试验发现这一点的,还是在某个地方记录了假/真返回?两者都不要提及。如果你有这种东西的可靠来源,欢迎分享。至少可以在MSDN上找到<代码>'作为一个论点,我刚刚用最新的Chrome和FF进行了测试,就像我说的:“似乎有效”。