Internet Explorer 11中的formatBlock

时间:2014-02-13 15:43:45

标签: internet-explorer formatting internet-explorer-11 execcommand

在使用execCommand和formatBlock格式化文本时,Internet Explorer中存在一个强制您使用尖括号括起元素名称的问题。 当Internet Explorer需要document.execCommand("formatBlock", false, "H1")

时,所有其他浏览器都会接受document.execCommand("formatBlock", false, "<H1>")

使用以下技巧可以检测到IE10及以下版本:

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

    return isIE;
}

Internet Explorer 11伪装成Firefox,打破了浏览器检测。我知道浏览器检测是不受欢迎的,我应该进行功能检测。在这种情况下,功能就在那里但不一致。

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

1 个答案:

答案 0 :(得分:1)

如果命令成功,则

execCommand()返回true,否则返回false。因此你可以这样做:

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

虽然其他浏览器似乎也适用于<H1> ......