IE条件语句 IE hack大全

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

软件测试面试如何测试一个杯子

本文主要介绍软件测试面试如何测试一个杯子,这里帮大家整理了详细的面试资料,和面试需要准备的知识点,有兴趣的小伙伴可以参考下
收藏 0 赞 0 分享

软件测试面试如何测试网页的登录页面

本文主要介绍软件测试面试如何测试网页的登录页面,这里整理了相关软件测试的一些基本知识,希望能帮助软件测试的同学
收藏 0 赞 0 分享

常见前端面试题及答案

本文是在GitHub上看到一个大牛总结的前端常见面试题,很多问题问的都很好,很经典、很有代表性。上面没有答案,我就整理了一下,从网上找了一些相关问题的答案
收藏 0 赞 0 分享

PHP和Java的主要区别有哪些?哪个最适合Web开发语言?

Java和PHP都是编程语言,大家知道它们最大的区别就是一个是静态语言一个是动态语言吧。没错,Java是一种静态语言,PHP是一种动态语言。那它们还有哪些区别? 哪个最适合Web开发语言?下面,小编再给大家详细介绍下。
收藏 0 赞 0 分享

玩转markdown 分享几个需要用到的工具

markdown是一个面向写作的语法引擎,markdown的最终目的都是解析成html用于网页浏览,所以它兼容html语法,即你可以在 markdown文档中使用原生的html标签
收藏 0 赞 0 分享

可能是最通俗的一篇介绍markdown的文章

这些日子一直在简书上使用markdown写作,已经渐渐的痴迷于这种简洁纯粹的写作方式了。不过就我逐渐入门markdown的写作过程来看,目前我看到的各种介绍markdown写作方式的文章都还略显极客,对于大多数像我一样没有基础的普通人来说,可能内容上的可接受性没有那么强
收藏 0 赞 0 分享

献给写作者的 Markdown 新手指南

Markdown 是一种「电子邮件」风格的「标记语言」,我们强烈推荐所有写作者学习和掌握该语言。为什么
收藏 0 赞 0 分享

github pull最新代码实现方法

本文主要介绍 github pull最新代码的资料,这里对 github pull最新代码做了详细流程介绍,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

GitHub Eclipse配置使用教程详解

本文主要介绍GitHub Eclipse,这里对Eclipse 使用GitHub的教程,图文并茂详细说明如何操作,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

Git 教程简单入门介绍

本文主要介绍Git 教程简单入门的东西,这里整理了Git 的基础资料和简单命令,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多