简体   繁体   English

为什么注释会影响文件的逻辑?

[英]Why do comments affect the logic of my file?

EDIT: 编辑:

In here, it shows this as being a comment. 在这里,它显示为注释。 In my IDE, it shows this as being code. 在我的IDE中,它显示为代码。 So weird (Code set #2): 所以很奇怪(代码集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>

I have two files. 我有两个文件。 One has comments and one does not. 一个有评论,一个没有。 The first set of code functions perfectly. 第一组代码功能完善。 The second set of code tells me Uncaught ReferenceError: $ is not defined in the JavaScript console, and the alert is not called. 第二组代码告诉我Uncaught ReferenceError: $ is not defined JavaScript控制台中Uncaught ReferenceError: $ is not defined ,并且未调用警报。 Why are the comments affecting my script? 为什么注释会影响我的脚本?

Code Set #1 代码集#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>

Code Set #2 代码集#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>

These are no ordinary comments, but conditional comments. 这些不是普通评论,而是有条件的评论。 See: http://www.quirksmode.org/css/condcom.html 请参阅: http//www.quirksmode.org/css/condcom.html

The comment above comments all javascript includes, thus they are not loaded, except in Internet Explorer lower than Version 9. 上面的注释注释了所有javascript包含的内容,因此,除了在低于版本9的Internet Explorer中,它们均未加载。

You get the error message because jquery is not loaded (it is inside the HTML comments). 您收到错误消息,因为未加载jquery(它位于HTML注释中)。 If you Use eg IE8 there won't be an error. 如果您使用例如IE8,就不会有错误。

<!--[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]-->

The if construct there is a conditional comment used for IE. if构造有一个用于IE的条件注释

The script tags will only be rendered if you're using an IE with a version number greater than 9. Every other browser will treat that whole section like a single comment and not include any of that javascript. 仅当您使用版本号大于9的IE时,才会呈现脚本标签。每个其他浏览器都将整个部分视为一个注释,而不包含任何JavaScript。

Your file is expecting jQuery to be loaded. 您的文件正在加载jQuery。 It seems that you commented out the jQuery script, you have to include the jQuery script, uncommented: 似乎您已注释掉jQuery脚本,您必须包括未注释的jQuery脚本:

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

It won't be run by the browser right? 它不会由浏览器运行吗? It will only be in every case streamed by the server, and downloaded by the client. 在任何情况下,它将仅由服务器流传输,并由客户端下载。

It shouldn't make any difference, as long as you don't have too many characters 只要您没有太多字符,就没有什么区别

Although dynamic pages generated server side, you might have to use server-side comment such as: <%-- comment --%> or {% comment %} 尽管动态页面在服务器端生成,但是您可能必须使用服务器端注释,例如:<%-comment-%>或{%comment%}

<!--[if lt IE 9]> & <![endif]--> are conditional comments for IE. <!--[if lt IE 9]><![endif]-->是IE的条件注释。 Other browsers will read them as comments. 其他浏览器会将其作为注释阅读。 If you are using an IE Version later than 9 these scripts will be loaded, which could be causing conflicts with other scripts. 如果您使用的是IE版本晚于9,则将加载这些脚本,这可能会导致与其他脚本发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM