Internet Explorer中有一个快速问题,当您使用execCommand和formatBlock格式化文本时,会迫使您用尖括号将元素名称括起来。document.execCommand("formatBlock", false, "H1")
Internet Explorer要求时,所有其他浏览器都接受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破坏了浏览器的检测。我知道浏览器检测很烦人,我应该进行特征检测。在这种情况下,功能存在,但不一致。
还有另一种方法可以不依赖浏览器检测而一致地应用块格式吗?
execCommand()
返回true
命令是否成功,false
否则返回。因此,您可以执行以下操作:
if (!document.execCommand('FormatBlock', null, 'H1')) {
document.execCommand('FormatBlock', null, '<H1>');
}
尽管其他浏览器似乎也可以使用<H1>
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句