imsobear / blog

果同学的博客
161 stars 9 forks source link

IE hack? forget it! #44

Closed imsobear closed 7 years ago

imsobear commented 10 years ago

IE hack? forget it!


IE hack,每个前端同学都避免不了的东西,你应该很了解下面的写法:

color: red\9\0; // ie9
color: red\0;  // ie8 & 9
*color: red;  // ie6&7
_color: red;  // ie6

每次碰到不同的 bug 我们总要考虑用那种 hack 处理,今天在 less 中写了一个 color: red\0 这样的 hack 时,打包失败,理论上这是 gulp-less 的 bug,不过问题是在这个 bug 之下我该怎么继续 hack,思考之后觉得这样的 hack 真的是烦透了,这样的解决方案也一定是不够靠谱的,相比来说,IE 条件注释会靠谱很多,那么借助 IE 的条件注释,hack 这件事简单了许多,如下:

<!--[if lt IE 7]>  <html class="ie ie6 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 7]>     <html class="ie ie7 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 8]>     <html class="ie ie8 lte9 lte8"> <![endif]-->
<!--[if IE 9]>     <html class="ie ie9 lte9"> <![endif]-->
<!--[if gt IE 9]>  <html> <![endif]-->
<!--[if !IE]><!--> <html> <!--<![endif]-->

如此的话,虽说 html 的源文件稍微有点恶心,好在 IE 条件注释在其他浏览器压根不会显示,所以这点恶心是可以接受的,之后我们再写 IE hack 就简单了许多,没有了奇葩的语法,csshint 也不会检测出什么问题,hack 写起来更加直观。

所以,忘掉那一堆 IE hack 吧!


扩展阅读: