在使用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,打破了浏览器检测。我知道浏览器检测是不受欢迎的,我应该进行功能检测。在这种情况下,功能就在那里但不一致。
是否有其他方法可以在不依赖浏览器检测的情况下始终如一地应用块格式化?
答案 0 :(得分:1)
execCommand()
返回true
,否则返回false
。因此你可以这样做:
if (!document.execCommand('FormatBlock', null, 'H1')) {
document.execCommand('FormatBlock', null, '<H1>');
}
虽然其他浏览器似乎也适用于<H1>
......