IE条件语句 IE hack大全

所属分类: 网络编程 / 其它综合 阅读数: 995
收藏 0 赞 0 分享
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.
更多精彩内容其他人还在看

浅析mmdetection在windows10系统环境中搭建过程

这篇文章主要介绍了mmdetection在windows10系统环境中搭建过程,本文图文并茂通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

so easy!10行代码写个"狗屁不通"文章生成器功能

这篇文章主要介绍了通过10行代码写个"狗屁不通"文章生成器功能,真的超级简单,感兴趣的朋友跟随脚本之家小编一起看看吧
收藏 0 赞 0 分享

Python实现用户登录并且输入错误三次后锁定该用户

这篇文章主要介绍了Python实现用户登录并且输入错误三次后锁定该用户,文中通过c#代码给大家补充介绍了密码输入三次错误后锁定用户功能,需要的朋友可以参考下
收藏 0 赞 0 分享

jetbrains mono字体安装方法(推荐)

JetBrains 推出了一款名为 "Mono" 的字体。按照官方的说法,Mono 是专为开发者打造的编程字体。这篇文章主要介绍了jetbrains mono字体安装方法,需要的朋友可以参考下
收藏 0 赞 0 分享

解决maven第一次创建项目太慢的问题

Maven是 Apache 下的一个纯 Java 开发的开源项目,是一个项目构建和管理的工具;它提供了帮助管理 构建、文档、报告、依赖、scms、发布、分发的方法。这篇文章主要介绍了maven第一次创建项目太慢的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

2019-nCoV 全国新型肺炎疫情每日动态趋势可视图

大家被新型冠状病毒搞的人心慌慌,每天宅在家里那也去不了,今天小编给大家分享2019-nCoV 全国新型肺炎疫情每日动态趋势可视图,需要的朋友可以参考下
收藏 0 赞 0 分享

详细解析Webpack是怎么运行的

这篇文章主要介绍了Webpack是怎么运行的,打包顺序是怎样的,非常基础且非常重要的知识点
收藏 0 赞 0 分享

Webpack基础教程之名词解释

webpack 是一个现代JavaScript 应用程序的静态模块打包器,这次为大家简单介绍一下webpack及关于webpack的一些专属名词入口(entry),输出(output),loader,插件(plugins)
收藏 0 赞 0 分享

将新型冠状病毒转二进制的代码(首发)

这篇文章主要介绍了新型冠状病毒转二进制的相关知识,分为java,js,php,pthon等语言的实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

解决VIM显示utf-8文件乱码问题

在Vim中,有四个与编码有关的选项,它们是:fileencodings、fileencoding、encoding和termencoding。下面,我们详细介绍一下这四个选项的含义和作用,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享
查看更多