IE条件语句 IE hack大全

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)

相关推荐

  • IE条件语句 IE hack大全

    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 co

  • SQL Server 的 SQL 语句导入导出大全

    MS SQL Server数据库SQL语句导入导出大全,包括与其他数据库和文件的数据的导入导出. /*******  导出到excel     EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:temp1.xls -c -q -S"GNETDATA/GNETDATA" -U"sa" -P""'     /***********  导入Excel     SELECT *     FR

  • 高性能JavaScript循环语句和条件语句

    一.循环语句 众所周知,常用的循环语句有for.while.do-while以及for-in,forEach.除了for-in和forEach性能略低外,平时我们对前三者的选择更多的是基于需求而非性能考虑,今天我们就对它们各自的性能做个测试,告诉我们最极端的情况下还能做哪些优化. 首先我们来谈谈为何for-in和forEach会比其他的慢.for-in一般是用在对象属性名的遍历上的,由于每次迭代操作会同时搜索实例本身的属性以及原型链上的属性,所以效率肯定低下:而forEach是基于函数的迭代(需

  • VBScript教程 第七课使用条件语句

    VB教程 > 第七课使用条件语句 控制程序执行 使用条件语句和循环语句可以控制 Script 的流程.使用条件语句可以编写进行判断和重复操作的 VBScript 代码.在 VBScript 中可使用以下条件语句: · If...Then...Else 语句 · select Case 语句 使用 If...Then...Else 进行判断 If...Then...Else 语句用于计算条件是否为 True 或 False,并且根据计算结果指定要运行的语句.通常,条件是使用比较运算符对值或变量进行

  • Javascript简写条件语句(推荐)

    经常在各处牛人的代码中看到许多简写的条件表达语句,看了一些介绍这方面的文章,觉得3 ways 2 say if这篇文章(http://www.thomasfrank.se/3_ways_2_say_if.html)还不错.在这篇文章中作者对传统的if...else....?:.&&/||三种条件表达的写法的特点及用处进行了总结归纳,简述如下: 1. if...else结构 // Set r to 0 or 1 var r= Math.floor(2*Math.random()) // Se

  • Python 专题二 条件语句和循环语句的基础知识

    前面讲述了"专题一.函数的基础知识",而这篇文章讲述的Python的条件语句和循环语句的基础知识.主要内容包括: 1.条件语句:包括单分支.双分支和多分支语句,if-elif-else 2.循环语句:while的使用及简单网络刷博器爬虫 3.循环语句:for的使用及遍历列表.元组.文件和字符串 前言: 语句块 在讲诉条件语句.循环语句和其他语句之前,先来补充语句块知识.(前面讲函数时已经用到过) 语句块并非一种语句,它是在条件为真(条件语句)时执行或执行多次(循环语句)的一组语句.在代

  • ASP基础入门第四篇(脚本变量、函数、过程和条件语句)

    在上一篇小编向大家简要介绍了 ASP 脚本语言之一 VBScript 的一些基本常识,本期将继续给大家讲解 VBScript 的脚本编写方法,并通过展示 VBScript 在 ASP 程序编写过程中的一系列实例使大家对 VBScript 有更进一层的理解.   函数和过程一样都是命名了的代码块,但它们却有很大的区别,过程完成程序任务,函数则返回值.我们可以这样理解,过程象一个完整的句子,而函数则象一个单词.举个例子,当你想获取某个数的平方根,你只要将该数传给 VBScript 的 Sqr() 函

  • Python的条件语句与运算符优先级详解

    Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false. Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条件: 执行语句-- else: 执行语句-- 其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围. el

  • VBS教程:VBScript 基础-使用条件语句

    使用条件语句控制程序执行使用条件语句和循环语句可以控制脚本的流程.使用条件语句可以编写进行判断和重复操作的 VBScript 代码.在 VBScript 中可使用以下条件语句: If...Then...Else 语句 Select Case 语句使用 If...Then...Else 进行判断If...Then...Else 语句用于计算条件是否为 True 或 False,并且根据计算结果指定要运行的语句.通常,条件是使用比较运算符对值或变量进行比较的表达式.有关比较运算符的详细信息,请参阅比

  • jquery if条件语句的写法

    //写个网页用了多门语言,脑袋转不过来亚! //代码: if(){} else if(){} else {} 以上这篇jquery if条件语句的写法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

随机推荐