优雅的 IE 条件注释新用法
<link rel="stylesheet" href="css/style.css" />
<!--[if IE]><link rel="stylesheet" href="css/ie.css" /><![endif]-->
<!--[if IE 6]><link rel="stylesheet" href="css/ie6.css" /><![endif]-->以上代码大家应该都了解,对蛋疼的 IE 使用额外的样式表解决蛋疼的 IE 兼容性问题。
这种方案有2个缺点:
- 对 IE 用户会增加请求数量,影响访问速度;
- 一个元素的样式存放在几个地方,不便于维护,容易出错。
所以大型网站都不会使用这个方案,要么一个通用样式表搞定,要么注释不会产生请求的内嵌样式表。
那么还有更好的方法么?以下这种写法是我在 W3C HTML5 Loge 上发现的:
<!--[if lt IE 7 ]> <html lang="zh-cn" class="ie6 ielt8"> <![endif]-->
<!--[if IE 7 ]> <html lang="zh-cn" class="ie7 ielt8"> <![endif]-->
<!--[if IE 8 ]> <html lang="zh-cn" class="ie8"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> <html lang="zh-cn"> <!--<![endif]-->这样我们就可以 easy 的在 CSS 里写:.ie6 body { /* Go to Hell! */ }。
然后 jQuery 里可以这么写:if ($('.ie6').length) { /* Go to Hell! */ }。
很好很强大是吧?