IE条件语句 IE hack大全

所属分类: 网络编程 / 其它综合 阅读数: 1072
收藏 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 分享

科学知识:理解socket

这篇文章主要介绍了科学知识:理解socket,本文试图用简洁的语言说清楚socket的相关知识,以便理解,需要的朋友可以参考下
收藏 0 赞 0 分享

科学知识:同步、异步、阻塞和非阻塞区别

这篇文章主要介绍了科学知识:同步、异步、阻塞和非阻塞区别,本文分别讲解了这些概念,需要的朋友可以参考下
收藏 0 赞 0 分享

24种编程语言的Hello World程序

这篇文章主要介绍了24种编程语言的Hello World程序,包括熟知的Java、C语言、C++、C#、Ruby、Python、PHP等编程语言,需要的朋友可以参考下
收藏 0 赞 0 分享

科普:多线程与异步的区别

这篇文章主要介绍了科普:多线程与异步的区别,本文讲解了多线程和异步操作的异同、异步操作的本质、线程的本质、异步操作的优缺点、多线程的优缺点等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

网址(URL)支持的最大长度是多少?最大支持多少个字符?

这篇文章主要介绍了网址(URL)支持的最大长度是多少?最大支持多少个字符?本文总结了IIS、apache服务器及浏览器软件Internet Explorer、Firefox、Opera、chrome等主流的浏览器软件支持情况,需要的朋友可以参考下
收藏 0 赞 0 分享

RPC、RMI、SOAP的区别详解

这篇文章主要介绍了RPC、RMI、SOAP的区别详解,本文还同时讲解了RPC、SOAP、WSDL的关系,需要的朋友可以参考下
收藏 0 赞 0 分享

一张图告诉你计算机编程语言的发展历史

这篇文章主要介绍了一张图告诉你计算机编程语言的发展历史,也可看作是计算机的发展历史大事记,需要的朋友可以参考下
收藏 0 赞 0 分享

Flyway数据库版本控制的教程详解

这篇文章主要介绍了Flyway数据库版本控制的教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

github版本库使用详细图文教程(命令行及图形界面版)

今天我们就来学习github的使用,我们将用它来管理我们的代码,你会发现它的好处的,当然是要在本系列教程全部完成之后,所以请紧跟站长的步伐,今天是第一天,我们来学习如何在git上建立自己的版本仓库,并将代码上传到仓库中
收藏 0 赞 0 分享
查看更多