IE条件语句 IE hack大全

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

GitHub 热门:别再用 print 输出来调试代码了

本文给大家分享GitHub 热门:别再用 print 输出来调试代码了的详细解说,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

将来会是Python、Java、Golang三足鼎立的局面吗

python的优势在于数据处理和人工智能等方向,所以go只可能吞噬Java的份额,很难撼动Python的奶酪,所以将来会是Python、Java、Golang三足鼎立的局面吗
收藏 0 赞 0 分享

EventStore文件存储设计详解

ENode是一个CQRS+Event Sourcing架构的开发框架,这篇文章主要介绍了EventStore文件存储设计 ,需要的朋友可以参考下
收藏 0 赞 0 分享

简单实用的aixcoder智能编程助手开发插件推荐

本文给大家分享一款简单实用的aixcoder智能编程助手开发插件推荐 ,需要的朋友可以参考下
收藏 0 赞 0 分享

分享4个最受欢迎的大数据可视化工具

大数据可视化是进行各种大数据分析解决的最重要组成部分之一。这篇文章主要介绍了4个最受欢迎的大数据可视化工具,需要的朋友可以参考下
收藏 0 赞 0 分享

Spark在Windows下的环境搭建方法

这篇文章主要介绍了Spark在Windows下的环境搭建方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

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

这篇文章主要介绍了Django 使用 cookie 实现简单的用户管理功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

eBay 打造基于 Apache Druid 的大数据实时监控系统

Apache Druid 是一个用于大数据实时查询和分析的高容错、高性能开源分布式时序数据库系统,旨在快速处理大规模的数据,并能够实现快速查询和分析。这篇文章主要介绍了eBay 如何打造基于 Apache Druid 的大数据实时监控系统?需要的朋友可以参考下
收藏 0 赞 0 分享

人工智能(AI)首选Python的原因解析

Python虽然是脚本语言,但是因为容易学,迅速成为科学家的工具。Python 已经是数据分析和 AI的第一语言,网络攻防的第一黑客语言,正在成为编程入门教学的第一语言,云计算系统管理第一语言
收藏 0 赞 0 分享

Hadoop分布式集群的搭建的方法步骤

这篇文章主要介绍了Hadoop分布式集群的搭建的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多