HTML 註釋語言javascript,css 詳細細節

不少人應該也和我同樣遇到過這樣的問題,須要去判斷瀏覽器版本或則在javascript 裏面去作複雜的邏輯處理來按需加載咱們的js,css.javascript

不過html 自帶的這一註釋標記子認爲人有用,或許這本是入門級的知識了,翻出來看看也不錯。css

下面就是IE中的HTML條件註釋的樣子,注意它使用的結束分隔符的技巧。html

<!--[if IE]> 
This content is actually inside an HTML comment. 
It will only be displayed in IE. 
<![endif]--> 
<!--[if gte IE 6]> 
This content will 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.java

Conditional comments are also supported by IE's Javascript interpreter, and C and C++ programmers may find them similar to the #ifdef/#endif functionality of the C preprocessor. A Javascript conditional comment in IE begins with the text /*@cc_on and ends with the text @*/. (The cc in cc_on stands for conditional compilation.) The following conditional comment includes code that is executed only in IE:程序員

IE的Javascript解析器也支持條件註釋,C和C++程序員可能會以爲它們和C處理器的 #ifdef/#endif 功能很相似。Javascript條件註釋使用字符/*@cc_on標記開始,用 @*/標記結束。
web

/*@cc_on瀏覽器

   @if (@_jscript)ide

     // This code is inside a JS comment but is executed in IE.this

     alert("In IE");spa

   @end

   @*/

Inside a conditional comment, the keywords @if @else , and @end delimit the code that is to be conditionally executed by IE's Javascript interpreter. Most of the time, you need only the simple conditional shown above: @if (@_jscript) . Jscript is Microsoft's name for its Javascript interpreter, and the @_jscript variable is always true in IE. 

在條件註釋內部,關鍵字 @if @else @end 區劃出哪些是要被IE的Javascript解析器條件執行的。大多數時候,只須要上面所示的簡單條件: @if (@_jscript) .Jscript是微軟本身的Javascript解析器的名字, @_jscript 變量在IE中的值恆爲ture

With clever interleaving of conditional comments and regular Javascript comments, you can set up one block of code to run in IE and a different block to run in all other browsers:

經過條件註釋和常規的Javascript註釋的合理交叉結合,能夠設置讓IE和其它瀏覽器運行不一樣的代碼。

/*@cc_on

   @if (@_jscript)

     // 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 
     // except IE will run this code. 
     alert('You are not using Internet Explorer');

/*@end

   @*/

Conditional comments, in both their HTML and Javascript forms, are completely nonstandard. They are sometimes a useful way to achieve compatibility with IE, however.

包括HTML形式和Javascript形式中的條件註釋,都是徹底非標準的,但它們有時是兼容IE的頗有用的方式。

相關文章
相關標籤/搜索