IE条件语句 IE hack大全

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

程序员的新年祝福 Happy New Year

再见,2017,你好,2018!祝大家在新的一年里,快快乐乐,编程技术更上一层楼,元旦快乐
收藏 0 赞 0 分享

新手程序员编程必不可少的工具

这篇文章主要为大家详细介绍了新手程序员编程必不可少的工具,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

关于爬虫和反爬虫的简略方案分享

这篇文章主要给大家介绍了一些关于爬虫和反爬虫的简略方案的相关资料,文中介绍的非常详细,对大家理解和学习爬虫与反爬虫具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

互联网科技大佬推荐的12本必读书籍

12本互联网科技大佬推荐的必读书籍,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

基于Token的身份验证的方法

这篇文章主要介绍了基于Token的身份验证的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详细介绍HTTP Cookie

本篇文章主要介绍了详细介绍HTTP Cookie,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

2018年最值得一读的互联网书单

2018年已经过去了近三分之二了,是不是感觉时光匆匆、一去不复返,过去的时间我们追不回,但是我们可以更加珍惜以后的时间!下面这篇文章主要给大家分享了2018年最值得一读的互联网书单,希望以后的日子里大家可以多读书,读好书
收藏 0 赞 0 分享

基于Token的身份验证之JWT基础教程

JWT(json web token)是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准。下面这篇文章主要给大家介绍了关于基于Token的身份验证之JWT的基础相关资料,文中通过示例代码的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

AERGO SHIP:用于开发智能合约的包管理器

AERGO SHIP:用于开发智能合约的包管理器,用于构建、测试和部署分布式应用程序的客户端框架和开发环境
收藏 0 赞 0 分享

看完工资立马翻3倍!(非程序员勿看)

负责粘贴你也可以做到!
收藏 0 赞 0 分享
查看更多