IE条件语句 IE hack大全

网络编程 2021-07-05 13:20www.168986.cn编程入门
条件注释只有在IE浏览器被支持,因此仅适用于IE的特殊指令。从IE5起都支持
Conditional ments work as follows:
代码如下:

<!--[if IE 6]>Special instructions for IE 6 here<![endif]-->
[code]

Their basic structure is the same as an HTML ment (<!-- -->). Therefore all other browsers will see them as normal ments 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 ment as if it were normal page content.
Since conditional ments use the HTML ment 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 ment referring to an extra style sheet.
Example
Below I added a lot of conditional ments that print out messages aording to your IE version.

Note however, that if you use multiple Explorers on one puter, the conditional ments 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 ments that reveal the IE version you're using.

Aording to the conditional ment this is IE
Aording to the conditional ment this is IE 7
Aording to the conditional ment this is IE lower than 9
Aording to the conditional ment this is IE lower or equal to 7
Aording to the conditional ment this is IE greater than 6

Code
The syntax I use is:
[code]
<p class="aent">
<!--[if IE]>
Aording to the conditional ment this is IE<br />
<![endif]-->
<!--[if IE 6]>
Aording to the conditional ment this is IE 6<br />
<![endif]-->
<!--[if IE 7]>
Aording to the conditional ment this is IE 7<br />
<![endif]-->
<!--[if IE 8]>
Aording to the conditional ment this is IE 8<br />
<![endif]-->
<!--[if IE 9]>
Aording to the conditional ment this is IE 9<br />
<![endif]-->
<!--[if gte IE 8]>
Aording to the conditional ment this is IE 8 or higher<br />
<![endif]-->
<!--[if lt IE 9]>
Aording to the conditional ment this is IE lower than 9<br />
<![endif]-->
<!--[if lte IE 7]>
Aording to the conditional ment this is IE lower or equal to 7<br />
<![endif]-->
<!--[if gt IE 6]>
Aording to the conditional ment this is IE greater than 6<br />
<![endif]-->
<!--[if !IE]> -->
Aording to the conditional ment 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 ments 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 ments 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 ments, too (though as yet none have done so), but they're unlikely to react to the specific query <!--[if IE]>.

I use conditional ments, 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) <ment> tag.

<p>This is <ment>not</ment> IE.</p>

This isIE.

This tag might be a replacement for the !IE conditional ment.

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by