为什么注释会影响我文件的逻辑?
Why do comments affect the logic of my file?
编辑:
在这里,它显示为评论。在我的 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>
我有两个文件。一个有评论,一个没有。第一组代码功能完美。第二组代码在 JavaScript 控制台中告诉我 Uncaught ReferenceError: $ is not defined
,并且未调用警报。为什么评论会影响我的脚本?
代码集 #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脚本,未注释:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!--[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 .
浏览器不会运行吧?它只会在任何情况下由服务器流式传输,并由客户端下载。
应该没有什么区别,只要你没有太多字符
虽然服务器端生成动态页面,但您可能必须使用服务器端注释,例如:<%-- comment --%> 或 {% comment %}
这些不是普通的评论,而是有条件的评论。
参见:http://www.quirksmode.org/css/condcom.html
以上评论全部javascript包含,因此不加载,除非在低于版本9的Internet Explorer中。
您收到错误消息是因为 jquery 未加载(它在 HTML 注释中)。如果你使用例如IE8不会报错
<!--[if lt IE 9]>
& <![endif]-->
是IE的条件注释。其他浏览器会将它们作为评论阅读。如果您使用的 IE 版本高于 9,将加载这些脚本,这可能会导致与其他脚本的冲突。
编辑:
在这里,它显示为评论。在我的 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>
我有两个文件。一个有评论,一个没有。第一组代码功能完美。第二组代码在 JavaScript 控制台中告诉我 Uncaught ReferenceError: $ is not defined
,并且未调用警报。为什么评论会影响我的脚本?
代码集 #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脚本,未注释:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!--[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 .
浏览器不会运行吧?它只会在任何情况下由服务器流式传输,并由客户端下载。
应该没有什么区别,只要你没有太多字符
虽然服务器端生成动态页面,但您可能必须使用服务器端注释,例如:<%-- comment --%> 或 {% comment %}
这些不是普通的评论,而是有条件的评论。 参见:http://www.quirksmode.org/css/condcom.html
以上评论全部javascript包含,因此不加载,除非在低于版本9的Internet Explorer中。
您收到错误消息是因为 jquery 未加载(它在 HTML 注释中)。如果你使用例如IE8不会报错
<!--[if lt IE 9]>
& <![endif]-->
是IE的条件注释。其他浏览器会将它们作为评论阅读。如果您使用的 IE 版本高于 9,将加载这些脚本,这可能会导致与其他脚本的冲突。