日期:2014-05-16  浏览次数:20316 次

JavaScript的注释
JavaScript 注释

可以添加注释来对 JavaScript 进行解释,或者提高其可读性。

1. 单行注释

单行的注释以 // 开始。

本例用单行注释来解释代码:

<script type="text/javascript">
// 这行代码输出标题:
document.write("<h1>This is a header</h1>");
// 这行代码输出段落:
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
</script>

2. JavaScript 多行注释

多行注释以 /* 开头,以 */ 结尾。

本例使用多行注释来解释代码:

<script type="text/javascript">
/*
下面的代码将输出
一个标题和两个段落
*/
document.write("<h1>This is a header</h1>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
</script>

我们也可以利用注释阻止程序的执行:
单行注释:
<script type="text/javascript">
document.write("<h1>This is a header</h1>");
document.write("<p>This is a paragraph</p>");
//document.write("<p>This is another paragraph</p>");
</script>

多行注释:
<script type="text/javascript">
/*
document.write("<h1>This is a header</h1>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
*/
</script>