javascript - 这个 comment/*@cc_on!@*/0 在 javascript 的 if 语句中做什么?

标签 javascript html internet-explorer

关于我在一本关于 HTML5 的电子书上找到的评论的一个小问题: /*@cc_on!@*/0 这个评论应该以某种方式绑定(bind)到 IE 浏览器的识别,以便使用 document.createElement() 来创建无法识别的 HTML5 元素,但我没有找不到有关其工作原理含义的有用信息,因为即使作者也没有解释它。有人可以向我解释一下它是什么以及它的作用吗?

感谢关注!

编辑:

作者在电子书中说:

下一个示例演示如何解决 HTML5 中引入的所有新元素的问题。这里我们包含了所有我们想要强制 IE 识别的元素:

代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>Styling Unknown Elements - 3</title>
    <script>
        (function() {
            if (! /*@cc_on!@*/ 0)
                return;

            var e = "abbr,article,aside,audio,canvas,  datalist,details,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output, progress,section,time,video".split(','),
                i = e.length;
            while (i--) {
                document.createElement(e[i]);

            }
        })()
    </script>
    <style>
        time {
            font-style: italic;
        }
    </style>
      ...

抱歉,缩进太可怕了,但我使用的是平板电脑。不管怎样,请查看脚本标签和 if 条件。

最佳答案

@cc_on语句IE(<11)浏览器的条件编译标志

引自 MSDN

@cc_on 语句(JavaScript)

Activates conditional compilation support within comments in a script.

注意

Conditional compilation is not supported in Internet Explorer 11 Standards mode and Windows Store apps. Conditional compilation is supported in Internet Explorer 10 Standards mode and in all earlier versions.

/*@cc_on @*/
/*@
    document.write("JavaScript version: " + @_jscript_version + ".");
    document.write("<br />");
    @if (@_win32)
        document.write("Running on the 32-bit version of Windows.");
    @elif (@_win16)
        document.write("Running on the 16-bit version of Windows.");
    @else
        document.write("Running on a different operating system.");
    @end
@*/

所以

if(!/*@cc_on!@*/0)

如果您的浏览器不支持条件编译(IE < 11 除外),则表达式为:

if(!0) // ==> TRUE

否则会是:

if(!!0) // ==> FALSE

关于javascript - 这个 comment/*@cc_on!@*/0 在 javascript 的 if 语句中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24473882/

相关文章:

javascript - 通过 &lt;script src =""> 加载 JavaScript ES6 模块并在 &lt;script&gt; 标记中导入所有导出是否相同?

Javascript 界面不适用于 android 4.2

html - 高度没有改变/在我的网站上工作

html - 一些 HTML5 标签有什么用?

html - Internet Explorer 中难看的字体呈现

Javascript 设置浏览器窗口大小

javascript - EmberJS - 具有 hasMany 关系的记录无法加载

Javascript:提交文本区域的更改不起作用

internet-explorer - 为什么这个 css 样式在 IE 8 中不起作用?

internet-explorer - AngularJS: "Error: Unexpected call to method or property access.undefined"仅在 IE 8 中