IE条件评论和Chrome / Firefox [英] IE Conditional Comments and Chrome/Firefox

查看:149
本文介绍了IE条件评论和Chrome / Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <! -  [if gt IE 7]> 
这是一些代码。
<![endif] - >

这可以很好地保持代码在任何低于8的IE中呈现。

但是,这也使代码不能在Chrome和Firefox中呈现。



关于这种情况发生的原因以及如何让代码在浏览器中呈现而不是IE?

解决方案

条件注释是Microsoft IE特有的规则,它们不是任何标准的一部分。如果您检查条件注释的结构:

 <! -  [if gt IE 7]> 
这是一些代码。
<![endif] - >

顾名思义,这只是一个大评论< ! - 评论 - > 。 IE检查这些条件的注释,这些也不符合HTML标准。



创建在IE中不渲染,但在其他渲染中渲染的代码浏览器,您使用以下条件:

 <! -  [if!IE]> - > 
这将由除IE以外的任何东西呈现。
<! - <![endif] - >

查看封闭注释中的条件?这就是为什么在普通浏览器中呈现,而IE检查条件,并决定省略一切,直到 endif



编辑

如果要添加其他条件,并在非IE浏览器上继续呈现代码,可以使用以下解决方法:

 <! -  [if gt IE 7]> <! -   - > 
下面是除了IE 7及其之外的任何代码。
<! - <![endif] - >

注意我不得不再次打开注释以防止IE渲染 - - > 代码前。其他浏览器仍然会将其视为评论的一部分。


I am using the following IE conditional comment:

<!--[if gt IE 7]>
Here is some code.
<![endif]-->

This works great to keep the code from rendering in any IE lower than 8.
However, this also keeps the code from rendering in Chrome and Firefox.

Any ideas on why this is happening, and how I can get the code to render in browsers other than IE?

解决方案

Conditional comments are a Microsoft IE-specific rule, and they are not part of any standard. If you check the structure of a conditional comment:

<!--[if gt IE 7]>
Here is some code.
<![endif]-->

As its name would imply, it is all just a big comment <!-- comment -->. IE checks comments for conditions such as these which, again, do not comply with HTML standards.

To create code which doesn't render in IE, but does render in other browsers, you use the following conditional:

<!--[if !IE]> -->
This will be rendered by anything but IE.
<!-- <![endif]-->

See how the conditions are enclosed in closed comments? That's why that is rendered in normal browsers, while IE checks for the conditional, and decides to omit everything up until the endif.

EDIT

If you want to add another condition, and keep rendering the code on non-IE browsers, you could use the following workaround:

<!--[if gt IE 7]> <!-- -->
Here is some code for anything but IE 7 and below.
<!-- <![endif]-->

Note I had to use open the comment again to prevent IE from rendering --> before the code. Other browsers will still consider it part of the comment.

这篇关于IE条件评论和Chrome / Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆