javascript - Javascript 中的有效注释
问题描述
我正在学习Javascript并遇到了这个:
<head>
<title>Test</title>
<script language="JavaScript">
<!--
document.write("Hi!");
//-->
</script>
</head>
这是一种有效的评论方式吗?为什么要使用它?
解决方案
这是一个HTML 注释,在脚本中使用这种格式可以追溯到 90 年代,当时一些浏览器不理解 JavaScript,因此我们必须从这些浏览器中隐藏 JS
通常
<script type="text/javascript"><!-- // hide from older browsers
document.write("Yes we have no bananas"); // this will be written if the browser understands JS
// --></script>
它在 JS 中作为注释被忽略,因此内容将被执行
除非代码就在后面<--
<script type="text/javascript">
<!-- document.write("No we have no bananas"); // this will NOT be written since it is treated as a comment in JS too
// --></script>
您将看到结束注释也在 JS 中被注释,因此它不会被视为减号减号大于// -->
JS 中的 VALID 多行注释是
/* these are
comments */
单行注释是
// here is one comment
推荐阅读
- macos - WKWebView 显示白条,直到窗口移动/调整大小
- azure - 什么 Azure 托管会自动扩展 .NET Core Web API 服务?
- r - R:使用 ggplot2 在分组条形图上居中误差条
- c# - 您的 SQL 语法有错误;在 Visual Studio 2019 Windows 窗体上查看与您的 mariadb 服务器对应的手册
- c# - 如何创建一个新类来检查图像类型?
- excel - 公式里面的公式
- python - 如何根据索引列表插入值?
- amazon-web-services - 如何使用 Ansible 执行跨账户配置?
- python - 如何在 Raspberry Pi 4 中为多个输入设备执行多线程
- scheme - 从列表中在球拍中构建 BST