为什么评论影响我的文件的逻辑? [英] Why do comments affect the logic of my file?

查看:134
本文介绍了为什么评论影响我的文件的逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:



在这里,它显示为注释。在我的IDE中,它显示为代码。很奇怪(代码集#2):

 < script src =https://oss.maxcdn.com/html5shiv /3.7.2/html5shiv.min.js\"&tt;/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>






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



<$ p $ <$>

p> <!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和Respond.js for IE8支持HTML5元素和媒体查询 - >
<! - [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>


解决方案

这些都不是普通的注释,
参见: http://www.quirksmode.org/css/condcom.html



上面的评论所有的javascript包括,因此他们没有加载,除了在低于版本9的Internet Explorer。



您得到错误消息,因为jquery没有加载(它在HTML注释中)。如果您使用例如IE8不会有错误。


EDIT:

In here, it shows this as being a comment. In my IDE, it shows this as being code. So weird (Code set #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. Why are the comments affecting my script?

Code Set #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

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

The comment above comments all javascript includes, thus they are not loaded, except in Internet Explorer lower than Version 9.

You get the error message because jquery is not loaded (it is inside the HTML comments). If you Use e.g. IE8 there won't be an error.

这篇关于为什么评论影响我的文件的逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆