IE的有条件注释详解(附实例代码)

<p class="descriptionArea">
<strong>IE的有条件注释</strong>是一种专有的(因此是非标准的)、对常规(X)HTML注释的Miscrosoft扩展。顾名思义,有条件注释使你能够根据条件(比如浏览器版本)显示代码块(不一定就是css,也可以是html代码,甚至是JavaScript代码)。尽管是非标准的,但是有条件注释对于其他所有浏览器作为常规注释出现,因此本质上是无害的。有条件注释在Windows上的IE5中首次出现,并且得到了Widnows浏览器所有后续版本的支持。
</p>
<p>IE的有条件注释是一种专有的(因此是非标准的)、对常规(X)HTML注释的Miscrosoft扩展。顾名思义,有条件注释使你能够根据条件(比如浏览器版本)显示代码块。尽管是非标准的,但是有条件注释对于其他所有浏览器作为常规注释出现,因此本质上是无害的。有条件注释在Windows上的IE5中首次出现,并且得到了Widnows浏览器所有后续版本的支持。</p>
<p><abbr title="Internet Explorer">IE</abbr>的有条件注释及其有效,而且非常容易记住。主要的缺点是这些注释需要放在HTML页面中,而不是放在CSS中。这样,当你不需要这些东西,或者有所更改的时候,就需要维护很多的地方。</p>
<p>让我们据几个例子来看看吧——</p>
<h3>只有IE才能识别</h3>
<p>因为只有IE5以上的版本才开始支持IE条件注释,所有“只有IE”才能识别的意思是“只有IE5版本以上”才能识别。</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if IE]></span></pre>
<pre class="alteven"><span class="rem"><style type="text/css"></span></pre>
<pre class="alt"><span class="rem"> @import "test.css";</span></pre>
<pre class="alteven"><span class="rem"></style></span></pre>
<pre class="alt"><span class="rem"><![endif]--></span></pre>
</div>
</div>
<br><h3>只有特定版本才能识别</h3>
<p>例如,只有IE5才能识别,只有IE6才能识别,只有IE7或者IE8才能识别。识别的条件是特定的版本,高了或者低了都不可以。例如下面的代码块,将只会在IE8中有效</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if IE 8]></span></pre>
<pre class="alteven"><span class="rem"><style type="text/css"></span></pre>
<pre class="alt"><span class="rem"> @import "test.css";</span></pre>
<pre class="alteven"><span class="rem"></style></span></pre>
<pre class="alt"><span class="rem"><![endif]--></span></pre>
</div>
</div>
<img src="http://downloads.cnblogs.com/justinyoung/articleIMG/2009/IEcondition01.gif" alt="IE共存" style="margin-bottom: 25px;"><h3>只有不是特定版本的才能识别</h3>
<p>当然,IE浏览器需要在5以上的版本,才在讨论范围之内。例如下面的例子中,特定了IE7版本不能识别。</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if !IE 7]></span></pre>
<pre class="alteven"><span class="rem"><style type="text/css"></span></pre>
<pre class="alt"><span class="rem"> @import "test.css";</span></pre>
<pre class="alteven"><span class="rem"></style></span></pre>
<pre class="alt"><span class="rem"><![endif]--></span></pre>
</div>
</div>
<img src="http://downloads.cnblogs.com/justinyoung/articleIMG/2009/IEcondition05.gif" alt="IE共存" style="margin-bottom: 25px;"><h3>只有高于特定版本才能识别</h3>
<p>只有高于制定版本的IE浏览器才能识别的代码块。</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if gt IE 7]></span></pre>
<pre class="alteven"><span class="rem"><style type="text/css"></span></pre>
<pre class="alt"><span class="rem"> @import "test.css";</span></pre>
<pre class="alteven"><span class="rem"></style></span></pre>
<pre class="alt"><span class="rem"><![endif]--></span></pre>
</div>
</div>
<img src="http://downloads.cnblogs.com/justinyoung/articleIMG/2009/IEcondition01.gif" alt="IE共存"><p>有朋友会问,为什么IE7没有应用到效果呢?那是因为IE7等于IE7,而不是高于IE7。所有IE7也没有起效果。</p>
<h3>等于或者高于特定版本才能识别</h3>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if gte IE 7]></span></pre>
<pre class="alteven"><span class="rem"><style type="text/css"></span></pre>
<pre class="alt"><span class="rem"> @import "test.css";</span></pre>
<pre class="alteven"><span class="rem"></style></span></pre>
<pre class="alt"><span class="rem"><![endif]--></span></pre>
</div>
</div>
<img src="http://downloads.cnblogs.com/justinyoung/articleIMG/2009/IEcondition02.gif" alt="IE共存" style="margin-bottom: 25px;"><h3>只有低于特定版本的才能识别</h3>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if lt IE 7]></span></pre>
<pre class="alteven"><span class="rem"><style type="text/css"></span></pre>
<pre class="alt"><span class="rem"> @import "test.css";</span></pre>
<pre class="alteven"><span class="rem"></style></span></pre>
<pre class="alt"><span class="rem"><![endif]--></span></pre>
</div>
</div>
<img src="http://downloads.cnblogs.com/justinyoung/articleIMG/2009/IEcondition03.gif" alt="IE共存" style="margin-bottom: 25px;"><h3>等于或者低于特定版本的才能识别</h3>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if lte IE 7]></span></pre>
<pre class="alteven"><span class="rem"><style type="text/css"></span></pre>
<pre class="alt"><span class="rem"> @import "test.css";</span></pre>
<pre class="alteven"><span class="rem"></style></span></pre>
<pre class="alt"><span class="rem"><![endif]--></span></pre>
</div>
</div>
<img src="http://downloads.cnblogs.com/justinyoung/articleIMG/2009/IEcondition04.gif" alt="IE共存" style="margin-bottom: 25px;"><h3>关键词解释</h3>
<p>上面那些代码好像很难记的样子,其实只要稍微解释一下关键字就很容易记住了。</p>
<p><abbr title="Less than">lt</abbr> :就是Less than的简写,也就是小于的意思。</p>
<p><abbr title="Less than or equal to">lte</abbr> :就是Less than or equal to的简写,也就是小于或等于的意思。</p>
<p><abbr title="Greater than">gt</abbr> :就是Greater than的简写,也就是大于的意思。</p>
<p><abbr title="Greater than or equal to">gte</abbr>:就是Greater than or equal to的简写,也就是大于或等于的意思。</p>
<p><abbr title="不等于">!</abbr>:就是不等于的意思,跟javascript里的不等于判断符相同。</p>
<p>这样解释一下,是不是好记多了。</p>
<h3>关于IE条件注释的特别说明</h3>
<h4>只有IE才能识别哦——</h4>
<p>曾经看到过下面的代码,现在想起来不禁有点想笑。这样的代码有什么意义吗?</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if !IE]></span></pre>
<pre class="alteven"><span class="rem"><style type="text/css"></span></pre>
<pre class="alt"><span class="rem"> @import "test.css";</span></pre>
<pre class="alteven"><span class="rem"></style></span></pre>
<pre class="alt"><span class="rem"><![endif]--></span></pre>
</div>
</div>
<h4>不仅仅是CSS哦</h4>
<p>很长时间,我对这个都有一种误解——以为它就是用来根据浏览器不同载入不同css,从而解决样式兼容性问题的。其实,我错了。它其实可以做的更多。它可以保护任何代码块——HTML代码块、JavaScript代码块、服务器端代码……看看下面的代码。</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="rem"><!--[if IE]></span></pre>
<pre class="alteven"><span class="rem">你使用的是IE浏览器,还会弹出个框哦。</span></pre>
<pre class="alt"><span class="rem"><script type="text/javascript"></span></pre>
<pre class="alteven"><span class="rem"> alert("你使用的是IE浏览器!");</span></pre>
<pre class="alt"><span class="rem"></script></span></pre>
<pre class="alteven"><span class="rem"><![endif]--></span></pre>
</div>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值