IE条件语句 IE hack大全

 更新时间:2012年05月23日 19:17:04   作者:  
条件注释只有在IE浏览器被支持,因此仅适用于IE的特殊指令。从IE5起都支持
Conditional comments work as follows:
复制代码 代码如下:

<!--[if IE 6]>Special instructions for IE 6 here<![endif]-->
[code]

Their basic structure is the same as an HTML comment (<!-- -->). Therefore all other browsers will see them as normal comments and will ignore them entirely.
IE, though, has been programmed to recognize the special <!--[if IE]> syntax, resolves the ifand parses the content of the conditional comment as if it were normal page content.
Since conditional comments use the HTML comment structure, they can only be included in HTML files, and not in CSS files. I'd have preferred to put the special styles in the CSS file, but that's impossible. You can also put an entire new <link> tag in the conditional comment referring to an extra style sheet.
Example
Below I added a lot of conditional comments that print out messages according to your IE version.

Note however, that if you use multiple Explorers on one computer, the conditional comments will render as if all these Explorer versions are the highest Explorer version available on your machine (usually Explorer 6.0).

Test
Below are a few conditional comments that reveal the IE version you're using.

According to the conditional comment this is IE
According to the conditional comment this is IE 7
According to the conditional comment this is IE lower than 9
According to the conditional comment this is IE lower or equal to 7
According to the conditional comment this is IE greater than 6

Code
The syntax I use is:
[code]
<p class="accent">
<!--[if IE]>
According to the conditional comment this is IE<br />
<![endif]-->
<!--[if IE 6]>
According to the conditional comment this is IE 6<br />
<![endif]-->
<!--[if IE 7]>
According to the conditional comment this is IE 7<br />
<![endif]-->
<!--[if IE 8]>
According to the conditional comment this is IE 8<br />
<![endif]-->
<!--[if IE 9]>
According to the conditional comment this is IE 9<br />
<![endif]-->
<!--[if gte IE 8]>
According to the conditional comment this is IE 8 or higher<br />
<![endif]-->
<!--[if lt IE 9]>
According to the conditional comment this is IE lower than 9<br />
<![endif]-->
<!--[if lte IE 7]>
According to the conditional comment this is IE lower or equal to 7<br />
<![endif]-->
<!--[if gt IE 6]>
According to the conditional comment this is IE greater than 6<br />
<![endif]-->
<!--[if !IE]> -->
According to the conditional comment this is not IE<br />
<!-- <![endif]-->
</p>

Note the special syntax:

gt: greater than
lte: less than or equal to
Also note the last one. It has a different syntax, and its contents are shown in all browsers that are not IE:
复制代码 代码如下:

<!--[if !IE]> -->


CSS hack?
Are conditional comments CSS hacks? Strictly speaking, yes, since they can serve to give special style instructions to some browsers. However, they do not rely on one browser bug to solve another one, as all true CSS hacks do. Besides, they can be used for more than CSS hacks only (though that rarely happens).

Since conditional comments are not based on a browser hack but on a deliberate feature I believe they are safe to use. Sure, other browsers could implement conditional comments, too (though as yet none have done so), but they're unlikely to react to the specific query <!--[if IE]>.

I use conditional comments, though sparingly. First I see if I can find a real CSS solution to an Explorer Windows problem. If I can't, though, I don't hesitate to use them.

Comment tag
A reader told me IE also supports the (non-standard) <comment> tag.

<p>This is <comment>not</comment> IE.</p>

This isIE.

This tag might be a replacement for the !IE conditional comment.

相关文章

  • Iris 环境搭建详细教程(最新版Go&IDEA&IrisV12)

    Iris 环境搭建详细教程(最新版Go&IDEA&IrisV12)

    这篇文章主要介绍了Iris 环境搭建(最新版Go&IDEA&IrisV12),本文通过示例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-11-11
  • Loongnix安装PyCharm Community 2020.2.3的教程详解

    Loongnix安装PyCharm Community 2020.2.3的教程详解

    这篇文章主要介绍了Loongnix安装PyCharm Community 2020.2.3的教程详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Django 使用 cookie 实现简单的用户管理功能

    Django 使用 cookie 实现简单的用户管理功能

    这篇文章主要介绍了Django 使用 cookie 实现简单的用户管理功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06
  • 微信应用号(小程序)入门安装教程及IDE(破解版)下载

    微信应用号(小程序)入门安装教程及IDE(破解版)下载

    这篇文章主要介绍了微信应用号(小程序)入门安装教程及IDE下载的相关资料,需要的朋友可以参考下
    2016-09-09
  • Windows10使用Anaconda安装Tensorflow-gpu的教程详解

    Windows10使用Anaconda安装Tensorflow-gpu的教程详解

    Anaconda是一个方便的python包管理和环境管理软件,一般用来配置不同的项目环境。这篇文章主要介绍了Windows10使用Anaconda安装Tensorflow-gpu的教程,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • HTTP头信息总结

    HTTP头信息总结

    这篇文章主要介绍了HTTP头信息总结,HTTP的头域包括通用头、请求头、响应头和实体头四个部分,本文罗列了40个HTTP头信息,需要的朋友可以参考下
    2014-07-07
  • 解决Visual Studio 2019本地不能运行Azure Functions

    解决Visual Studio 2019本地不能运行Azure Functions

    本文主要介绍了Visual Studio 2019本地不能运行Azure Functions的解决方方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • 8种类型极品程序员,不知你属于哪一种?

    8种类型极品程序员,不知你属于哪一种?

    这篇文章主要介绍了8种类型极品程序员,不知你属于哪一种?本文总结归纳了8种程类型的程序员,总结的非常精辟,来看看你是属于哪一种吧~
    2014-09-09
  • 嘁,都2020了,你咋还在单纯的使用if-else

    嘁,都2020了,你咋还在单纯的使用if-else

    我们在写程序时,常常需要指明两条或者更多的执行路径,使得程序执行时,能够选择其中一条路径,去执行相应的语句,产生对应的结果 —— 这也是条件语句在程序中的作用。下面通过例子给大家介绍下if-else的使用,感兴趣的朋友一起看看吧
    2020-10-10
  • 微信小程序开发工具介绍

    微信小程序开发工具介绍

    这篇文章主要为大家介绍了微信小程序开发工具及基本的使用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04

最新评论