When would <!--[if !IE]> be used?

微笑、不失礼 提交于 2019-12-10 13:34:37

问题


I was pondering this question last night while debugging some things in IE7, when I typed in <!--[if IE7]>... it occured to me that I've seen <!--[if !IE]>.

Now what get me is that as far as I understand it, only IE recognises these conditional comments, so to to say if NOT IE makes no sense, does IE see this and say "Iam IE, so this doesn't apply to me?" or are people getting confused with which browsers can recognise it?

So my question is.

Where would you use <!--[if !IE]> and what is its purpose?


回答1:


AFAIK, other browsers treat conditional comments as comment blocks (ie, they aren't parsed at all). So, such a comment will make something happen (ie, include a CSS file) in anything EXCEPT Internet Explorer (so it's the inverse of the [if IE] conditional comments). Add all your CSS in such comments and see how IE behaves :P.

[EDIT]

OK, I just checked. This is indeed silly. Since the <style> tag is in the CC, other browsers don't parse it at all, so it effectively doesn't get included at all. IE indeed interprets this as "aha, I am IE so I need to skip this". The only reasonable explanation I can think of is Microsoft assuming at some point other browsers might start parsing conditional comments.




回答2:


You can use it with a special synax:

<!--[if !(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->

This means that IE ignores what is in between, but other browsers treat the conditional comments as closed comments and interpret what's in between.




回答3:


I would say it would be if you wanted to run a script that works in every browser other than IE, as IE would not run it...




回答4:


To confirm your suspicion it is kinda pointless to put it like that in your HTML, as most or all browsers will ignore the content of the comment block.

It can be useful to use the negation operator when wanting to exclude a specific version though, like <!--[if !(IE 6)]> would be parsed in all versions of IE (starting IE5), but not in IE6.

http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx



来源:https://stackoverflow.com/questions/4967205/when-would-if-ie-be-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!