一、HTML条件注释和javascript条件注释说明
条件注释主要是针对ie的不兼容性,通过条件注释实现以某种方式为ie编写代码,而按照另一种方式为其他浏览器编写代码。条件注释并不符合标准规范,但在处理不兼容性事非常有用。
二、HTML条件注释
html条件注释,经测试能被ie以外的浏览器识别,输出结果与预想一致。
举个栗子:
<!--[if IE]>
This content is actually inside an HTML comment.
It will only be displayed in IE.
<![endif]-->
<!--[if IE6]>
This content is actually only be displayed by IE 6 and later.
<![endif]-->
<!--[if !IE]><-->
This is normal HTML content,but IE will not display it because of
the comment above and the comment below.
<!--><![endif]-->
This is normal content,displayed by all browsers.
三、javascript条件注释
Javascript条件注释只被ie中JavaScript解释器支持。
举个栗子:
<script>
/*@cc_on
@if(@_jscript)
//This code is inside a JS comment but is executed in IE.
//This code is inside a conditional comment,which is also a
//regular Javascript comment.IE runs it but other browsers ignore it.
alert("You are using Internet Explorer");
@else*/
//This code is no longer inside a Javascript comment,but is still
//inside the IE conditional comment. This means that all browsers
//excepte IE will run this code.
alert("you are not using Internet Explorer");
/*@end
@*/
</script>
(以上案例参考Java权威指南Page 332)