【问题标题】:Why do comments affect the logic of my file?为什么评论会影响我的文件的逻辑?
【发布时间】:2015-05-04 12:53:57
【问题描述】:

编辑:

在这里,它显示为评论。在我的 IDE 中,它显示为代码。太奇怪了(代码集 #2):

<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="bootstrap-3.3.2-dist/js/bootstrap.min.js"></script>

我有两个文件。一个有 cmets,一个没有。第一组代码功能完美。第二组代码在 JavaScript 控制台中告诉我Uncaught ReferenceError: $ is not defined,并没有调用警报。为什么 cmets 会影响我的脚本?

代码集 #1

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="bootstrap-3.3.2-dist/js/bootstrap.min.js"></script>
</head>
<body>
<script>
    $(function () {
        alert("JQUERY!");
    });
</script>
</body>
</html>

代码集 #2

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="bootstrap-3.3.2-dist/js/bootstrap.min.js"></script>
    <![endif]-->
</head>
<body>
<script>
    $(function () {
        alert("JQUERY!");
    });
</script>
</body>
</html>

【问题讨论】:

  • 如果不包含jQuery,则不能使用jQuery($是jQuery创建的)。
  • 很奇怪。这在我的 IDE 中没有作为评论出现
  • 您注释掉包括 jQuery 脚本并期望 jQuery 能够工作。
  • 使用&lt;!--[if lt IE 9]&gt;,您已经注释掉了除 IE
  • 我猜你想在 jQuery &lt;script&gt; 之前移动 &lt;![endif]--&gt;

标签: javascript html comments


【解决方案1】:

&lt;!--[if lt IE 9]&gt; & &lt;![endif]--&gt; 是 IE 的条件 cmets。其他浏览器会将它们读取为 cmets。如果您使用的 IE 版本高于 9,则会加载这些脚本,这可能会导致与其他脚本发生冲突。

【讨论】:

    【解决方案2】:

    这些不是普通的 cmets,而是有条件的 cmets。 见:http://www.quirksmode.org/css/condcom.html

    上面的注释 cmets all javascript include, 因此它们不会被加载,除非在低于版本 9 的 Internet Explorer 中。

    您收到错误消息是因为未加载 jquery(它位于 HTML cmets 中)。如果你使用例如IE8不会出错。

    【讨论】:

      【解决方案3】:

      它不会被浏览器运行吧?它只会在每种情况下由服务器流式传输,并由客户端下载。

      应该没什么区别,只要你没有太多字符

      虽然动态页面在服务器端生成,但您可能必须使用服务器端注释,例如: 或 {% comment %}

      【讨论】:

        【解决方案4】:
        <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="bootstrap-3.3.2-dist/js/bootstrap.min.js"></script>
        <![endif]-->
        

        if 构造中有一个conditional comment 用于 IE。

        只有在您使用版本号大于 9 的 IE 时才会呈现脚本标签。其他所有浏览器都会将整个部分视为单个注释,并且不包含任何 javascript。

        【讨论】:

          【解决方案5】:

          您的文件需要加载 jQuery。看来您注释掉了 jQuery 脚本,您必须包含 jQuery 脚本,未注释:

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
          

          【讨论】:

          • 这是正确答案。脚本(包括 jQuery)仅在 IE 8 及以下版本中加载。这会导致所有其他浏览器出错。
          • 投反对票的不是我。但感谢布兰登。这有点道理
          • 我对你投了反对票,因为 OP 在问为什么 cmets 很重要,而你只是告诉他 jquery 没有脚本标签就无法工作。他已经知道了。你没有回答他的问题。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-04-13
          • 1970-01-01
          • 2021-01-06
          • 2013-07-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多